Useful AWK One-Liners


Useful AWK One-Liners

Awk is a great language for useful one-line programs. Below are a number of useful awk scripts that can be coded in a single line.

 # Emit every line that is not blank     awk 'NF > 0 {print}' file.txt     # Emit the number of lines in a file     awk '{num_lines++} END{print num_lines}' file.txt     # or (another number of lines example)     awk 'END { print NR }' file.txt     # Emit the first 10 lines of a file (like head)     awk 'NR < 11 { print 
 # Emit every line that is not blank     awk 'NF > 0 {print}' file.txt     # Emit the number of lines in a file     awk '{num_lines++} END{print num_lines}' file.txt     # or (another number of lines example)     awk 'END { print NR }' file.txt     # Emit the first 10 lines of a file (like head)     awk 'NR < 11 { print $0 }' file.txt     # Print each line, preceded by its line number     awk '{print NR, $0}' missiles.txt     # Count the number of lines that contain 'PATTERN'     awk '/PATTERN/{num++} END{ print num }' file.txt 
}' file.txt # Print each line, preceded by its line number awk '{print NR,
 # Emit every line that is not blank     awk 'NF > 0 {print}' file.txt     # Emit the number of lines in a file     awk '{num_lines++} END{print num_lines}' file.txt     # or (another number of lines example)     awk 'END { print NR }' file.txt     # Emit the first 10 lines of a file (like head)     awk 'NR < 11 { print $0 }' file.txt     # Print each line, preceded by its line number     awk '{print NR, $0}' missiles.txt     # Count the number of lines that contain 'PATTERN'     awk '/PATTERN/{num++} END{ print num }' file.txt 
}' missiles.txt # Count the number of lines that contain 'PATTERN' awk '/PATTERN/{num++} END{ print num }' file.txt



GNU/Linux Application Programming
GNU/Linux Application Programming (Programming Series)
ISBN: 1584505680
EAN: 2147483647
Year: 2006
Pages: 203
Authors: M. Tim Jones

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net