3

I have a file with many lines, such as :

blue:fish
red:tomato
black:moon

I would like the whole line to be deleted if after ":" there is less than 4 chars.

  • If any answer solves your issue, please consider accepting that answer. If you're happy with an answer, accepting it and maybe even upvoting it is the best way to say "Thank You!" :-) – Kusalananda Aug 30 '17 at 20:17
4

You can use sed:

sed -r -e '/:.{0,3}$/d' data

-r uses extended regular expressions (for {}). The sed expression matches all lines with a colon followed by zero to three characters and then the end of the line; d deletes those lines. All the rest will be printed.

2

This AWK command splits on : and prints lines which of which the second field is at least 4 chars (not less than 4 chars). Assumption: : is always present and does not occur multiple times.

awk -F : "length($2) >= 4"

If it occurs multiple times, then you could use $NF instead of $2 to refer to the last column.

0

There is many ways to do it.

Here is a perl:

$ perl -ne 'print unless /:.{0,3}$/' file
blue:fish
red:tomato
black:moon
0

In Bash:

while read; do
  [[ "$REPLY" =~ :.{4,} ]] && printf '%s\n' "$REPLY"
done

Then, redirect as you need.

0

Using grep:

grep -E ':.{4,}$' file

Using grep + sponge (from moreutils), to "edit" file "in place":

grep -E ':.{4,}$' file|sponge file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.