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.
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".
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With