TonyW
TonyW

Reputation: 18885

Skip lines whose 2nd column has a string

My CVS file has 10 columns and I want to get rid of lines whose 2nd field ($2) has string beginning with "synonymous". I tried this:

awk '$2 ~ /^synonymous/ {next}'

but it does not print out anything.

Upvotes: 2

Views: 145

Answers (3)

NPE
NPE

Reputation: 500683

next skips the record, but there's nothing in your code that prints anything.

You need to print the records that don't match the pattern:

awk '$2 ~ /^synonymous/ {next} {print}'

This can be simplified as follows:

awk '$2 !~ /^synonymous/ {print}'

Since print is the default command, this can be further simplified to

awk '$2 !~ /^synonymous/'

(HT @Jotne.)

Upvotes: 3

Ed Morton
Ed Morton

Reputation: 203995

You need to tell awk what you want to do. So far you've just told it to skip records whose 2nd field starts with "synonymous" but you haven't told awk to do anything at all with the rest of them. Try this instead:

awk '$2 !~ /^synonymous/'

That says "if the 2nd field doesn't start with synonymous, then invoke the default action of printing the record".

Upvotes: 4

Jotne
Jotne

Reputation: 41460

You need to print the line that do not contain synonymous, the 1 does the tric

awk '$2 ~ /^synonymous/ {next}1'

PS Solution Ed Morton posted is the best :)

Upvotes: 2

Related Questions