AWK Command Examples
AWK Command Examples for Text Processing
AWK is a powerful text-processing utility that excels at pattern scanning and processing. It reads input line by line and performs actions based on specified patterns. This cheat-sheet provides common AWK command examples to help you efficiently process and manipulate text data.
Basic Pattern Matching and Printing
| Command | Description |
|---|---|
awk '/pattern/ {print $1}' |
Prints the first field of lines matching 'pattern' in standard Unix shells. |
awk '/pattern/ {print "$1"}' |
Prints the first field of lines matching 'pattern' when compiled with DJGPP, Cygwin. |
awk "/pattern/ {print \"$1\"}" |
Prints the first field of lines matching 'pattern' for GnuWin32, UnxUtils, Mingw. |
Formatting and Spacing Text
| Command | Description |
|---|---|
awk '1;{print ""}' |
Doubles the spacing of a file by printing a blank line after each original line. |
awk 'BEGIN{ORS="\n\n"};1' |
Another method to double-space a file using the Output Record Separator (ORS). |
awk 'NF{print $0 "\n"}' |
Doubles the spacing of a file that already contains blank lines, ensuring consistent spacing. |
awk '1;{print "\n"}' |
Triples the spacing of a file by printing two blank lines after each original line. |
Line Numbering and Counting
| Command | Description |
|---|---|
awk '{print FNR "\t" $0}' files* |
Prepends each line with its line number within its respective file (FNR). |
awk '{print NR "\t" $0}' files* |
Prepends each line with its line number across all files processed together (NR). |
awk '{printf("%5d : %s\n", NR,$0)}' |
Numbers each line of a file with right-aligned, padded numbers. |
awk 'NF{$0=++a " :" $0};1' |
Numbers each non-blank line of a file, prepending the line number. |
awk 'END{print NR}' |
Counts the total number of lines in the input, emulating wc -l. |
awk '/Beth/{n++}; END {print n+0}' file |
Counts the total number of lines that contain the pattern "Beth". |
Field Manipulation and Calculations
| Command | Description |
|---|---|
awk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}' |
Prints the sum of all fields for each line. |
awk '{for (i=1; i<=NF; i++) s=s+$i}; END{print s}' |
Calculates and prints the sum of all fields across all lines in the input. |
awk '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }' |
Replaces each negative field with its absolute value and prints the modified line. |
awk '{for (i=1; i<=NF; i++) $i = ($i < 0) ? -$i : $i; print }' |
An alternative way to replace negative fields with their absolute values. |
awk '{ total = total + NF }; END {print total}' file |
Prints the total count of fields (words) across all lines in a file. |
awk '$1 > max {max=$1; maxline=$0}; END{ print max, maxline}' |
Finds and prints the largest value in the first field and the entire line containing it. |
awk '{ print NF ":" $0 }' |
Prints the number of fields followed by the content of each line. |
awk '{ print $NF }' |
Prints only the last field of each line. |
awk '{ field = $NF }; END{ print field }' |
Prints the last field of the very last line of the input. |
awk 'NF > 4' |
Prints lines that contain more than 4 fields. |
awk '$NF > 4' |
Prints lines where the value of the last field is greater than 4. |
Further Resources
- GNU AWK Manual
- MDN Web Docs: Date (for context on date/time processing)
- Stack Overflow: AWK Questions