O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Selective deletion of certain lines in a file with AWK

In this section, we look at the working of one-liners for the selective deletion of lines and fields in a file:

  • Delete all blank lines from a file (similar to grep): In this example, we use NF– the number of fields built-in variable in each line. For empty lines, NF evaluates to 0, that is, false. Since it is a false statement, the does not get printed; we get only non-empty lines in our output, as follows:
$ awk NF <FILENAME>

     Or:

$ awk '/./' label.dat

   

 Or:

$ awk '!/^$/' label.dat

    Or:

$ awk NF label.dat
  • Deleting consecutive duplicate lines from a file: In this example, we use a variable (here a) to store the current input line. The value stored in variable a is matched ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required