Reputation: 18885
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
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
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
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