I have a file (example below).  Some lines have five fields, where the fifth field is a number.  But a few lines in the file have only four fields.

How can we remove these lines with only four fields?  They are not aligned by columns.

delay  from  threshold/temp/atp/CK   rx/tx/sx/C14/CLR/DQ  5.0
delay  from  threshold2   rx/C10/CLR  7.0
delay  to    threshold10  rx10  3.0
delay  from  min/max/values/atp  tx/rx/mx/T13/CLR
delay  to  min5/atp2  tx/mx/P11/CTR

In the above example I want to remove the fourth and fifth lines.

I tried awk -F\| '$5~/\w/' file, but it did not help.

  • Specifying a file separator of '|' (assuming it was quoted) means all your lines have one field, because there are no separators. And if $5 contains a decimal point, it does not match \w (a word). But that test is reversed anyway. – Paul_Pedant Jul 13 '20 at 20:12
  • Also \w is a non-POSIX extension shorthand for [[:alnum:]_] that'll only work in some awks. – Ed Morton Jul 13 '20 at 23:46

awk '$5~/\w/' does work for me. It prints all lines where the fifth column matches the regex \w aka [a-zA-Z0-9_]. Make sure to specify the correct delimiter with the -F option.

awk 'NF>4' expresses your intention more clearly. It prints all lines whose Number of Fields is greater than 4.

  • Thanks a lot, awk 'NF>4' this works for my requirement – Partha Jul 13 '20 at 20:55

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.