Posts AWK Cheatsheet with Examples
Post
Cancel

AWK Cheatsheet with Examples

AWK is a powerful pattern scanning and processing command. It is especially useful for text and data extraction, manipulation, and reporting.

Basic Syntax

1
awk 'pattern { action }' file
  • pattern: condition to match (can be omitted)
  • action: what to do if pattern matches

Common Built-in Variables

Variable Description
$0 Entire current line
$1..$n First to nth field (column)
NR Number of records (lines) read so far
NF Number of fields in current record
FS Field separator (input)
OFS Output field separator

Frequently Used Examples

1. Print 1st Column of a File

1
awk '{print $1}' filename

2. Print Unique Values from Column

1
awk '{print $2}' filename | sort | uniq

3. Pipe Each Line to a Command

1
awk '{print $0}' filename | while read line; do echo "$line"; done

4. Add Delimiter for Columns

For example, use : as delimiter:

1
awk '{print $1 ":" $2}' filename

5. Ignore Lines Matching a Pattern

1
awk '!/pattern_to_ignore/' filename

Or, using an if condition:

1
awk '{ if ($1 != "skip") print }' filename

6. Delay Between Each Line Processing

Using a pipe to sleep:

1
awk '{print $1}' filename | while read line; do echo "$line"; sleep 1; done

Setting Custom Delimiters

  • Set Input Field Separator (e.g., comma):
1
awk -F',' '{print $1}' filename
  • Set Output Field Separator:
1
awk 'BEGIN{OFS=":"} {print $1, $2}' filename

Use Case: Filter, Format, and Execute

  • Example: Filter out comments and execute line
1
awk '!/^#/ {print $1}' script.txt | while read cmd; do eval "$cmd"; done

Handy Tips

  • awk scripts can be multiline using BEGIN, END, and main block.
  • Can be used in pipelines.
  • Fast for CSV and TSV file processing.

Happy AWKing!