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>
$ awk '/./' label.dat
$ awk '!/^$/' label.dat
$ 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 ...