Pattern scanning and text processing language for field-oriented data extraction, reporting, and transformation.
awk {flag-f} {flag-f} {flag-v} {flag-b} {flag-c} ๐ flag-f ๐ flag-v ๐ flag-b ๐ flag-c awk {flag-f} {flag-f} {flag-v} {flag-b} {flag-c} Sandbox: execFile · Timeout: 60s
text.process.field text.aggregate.count text.aggregate.average text.process.report data.transform.column text.transform.csv text.aggregate.sum security.execution.command security.privilege-escalation.shell system.file.read system.file.write | Name | Type | Required | Default | Description | Aliases |
|---|---|---|---|---|---|
flag-f | string | No | - | --file=progfile | -f |
flag-F | string | No | - | --field-separator=fs | -F |
flag-v | string | No | - | --assign=var=val | -v |
flag-b | string | No | - | --characters-as-bytes | -b |
flag-c | string | No | - | --traditional | -c |
flag-d | string | No | - | --dump-variables[=file] | -d |
flag-D | string | No | - | --debug[=file] | -D |
flag-e | string | No | - | --source='program-text' | -e, -t |
flag-E | string | No | - | --exec=file | -E |
flag-g | string | No | - | --gen-pot | -g |
flag-h | string | No | - | --help | -h |
flag-i | string | No | - | --include=includefile | -i |
flag-k | string | No | - | --csv | -k |
flag-l | string | No | - | --load=library | -l |
flag-L | string | No | - | --lint[=fatal|invalid|no-ext] | -L, -e |
flag-n | string | No | - | --non-decimal-data | -n |
flag-o | string | No | - | --pretty-print[=file] | -o |
flag-p | string | No | - | --profile[=file] | -p |
flag-r | number | No | - | --re-interval | -r |
flag-s | string | No | - | --no-optimize | -s |
flag-t | string | No | - | --lint-old | -t |
exec-expression | string | No | - | Execute shell command via system() or print|/bin/sh | - |
getline-variable | string | No | - | Read file content into variable via getline < file | - |
M | boolean | No | - | -M, --bignum |
| Type | Description | MIME | Schema | Trust |
|---|---|---|---|---|
text.transformed | Processed text output | text/plain | - | verified |
data.report | Formatted report | text/plain | - | verified |
Inputs
text.plain Input text for processing
text.awk.script Awk script or expression
Outputs
text.transformed Processed text output
Produces
text.transformedConsumes
text.plaintext.awk.script| Resource | Value |
|---|---|
| CPU | low |
| Memory | 16 MB |
| Network | none |
| Disk I/O | low |
Print specific columns from a file
awk '{print $1, $3}' file.txt Print with custom field separator (CSV)
awk -F',' '{print $1, $2}' data.csv Filter rows by column value
awk '$3 > 100 {print $1, $3}' data.txt Compute sum of a column
awk '{sum += $2} END {print sum}' data.txt Print formatted report with headers
awk 'BEGIN {print "Name\tScore"} {print $1 "\t" $2}' data.txt Count lines per category
awk '{count[$1]++} END {for (c in count) print c, count[c]}' data.txt Can be used to execute arbitrary commands on a system and spawn shells.
awk 'BEGIN{system("/bin/sh")}' Can be used to execute arbitrary commands on a system.
awk 'BEGIN {system("ls"); exit}' /dev/null
The file must exist and command will be executed as many rows there are in the file.
awk 'system("ls")' /etc/passwd
If spaces cannot be inserted, we can use `sprintf(%c,32)` to emulate them.
awk '//{}BEGIN{system(sprintf("uname%c-aa",32))}'
Read an arbitrary file.
awk 'BEGIN{while((getline line<"/etc/passwd")>0){print line}}' /dev/null
Argument injection: execute arbitrary command: Can be used to execute arbitrary commands on a system.
awk 'BEGIN {system("ls"); exit}' /dev/null Argument injection: execute arbitrary command: The file must exist and command will be executed as many rows there are in the file.
awk 'system("ls")' /etc/passwd Argument injection: execute arbitrary command: If spaces cannot be inserted, we can use `sprintf(%c,32)` to emulate them.
awk '//{}BEGIN{system(sprintf("uname%c-aa",32))}' Argument injection: read local file: Read an arbitrary file.
awk 'BEGIN{while((getline line<"/etc/passwd")>0){print line}}' /dev/null Argument injection: read local file: Print the contents of multiple files.
awk '//' /etc/passwd /etc/hostname /root/.ssh/id_rsa Argument injection: write to local file: Write to an arbitrary file
awk 'BEGIN{print "ssh-rsa ..." > "/root/.ssh/authorized_keys}' /dev/null Sum integers from a file or STDIN, one integer per line.
printf '1\n2\n3\n' | awk '{sum += $1} END {print sum}' Using specific character as separator to sum integers from a file or STDIN.
printf '1:2:3' | awk -F ":" '{print $1+$2+$3}' Print line number 12 of file.txt
awk 'NR==12' file.txt Print first field for each record in file.txt
awk '{print $1}' file.txt cheat.sheets: awk
Print only lines that match regex in file.txt cheat.sheets: awk
awk '/regex/' file.txt Print only lines that do not match regex in file.txt
awk '!/regex/' file.txt Print any line where field 2 is equal to "foo" in file.txt
awk '$2 == "foo"' file.txt Print lines where field 2 is NOT equal to "foo" in file.txt
awk '$2 != "foo"' file.txt Print line if field 1 matches regex in file.txt
awk '$1 ~ /regex/' file.txt Print line if field 1 does NOT match regex in file.txt
awk '$1 !~ /regex/' file.txt Print lines with more than 80 characters in file.txt
awk 'length > 80' file.txt Print a multiplication table.
awk -v RS='' ' cheat.sheets: awk
{ cheat.sheets: awk
for(i=1;i<=NF;i++){ cheat.sheets: awk
printf("%dx%d=%d%s", i, NR, i*NR, i==NR?"\n":"\t") cheat.sheets: awk
} cheat.sheets: awk
} cheat.sheets: awk
' <<< "$(seq 9 | sed 'H;g')" Specify output separator character.
printf '1 2 3' | awk 'BEGIN {OFS=":"}; {print $1,$2,$3}' Search paragraph for the given REGEX match. Paragraphs will be collapsed together.
awk -v RS='' '/42B/' file Search paragraph for the given REGEX match. Paragraphs will be separated with a new line.
awk -v RS= ORS='\n\n' '/42B/' file Display only first field in text taken from STDIN.
echo 'Field_1 Field_2 Field_3' | awk '{print $1}' Use AWK solo; without the need for something via STDIN.
awk 'BEGIN {print("Example text.")}' Access environment variables from within AWK.
awk 'BEGIN {print ENVIRON["LS_COLORS"]}' Count number of lines taken from STDIN.
free | awk '{L++} END {print(L)}' Cleaner, more efficient approach to the above.
free | awk 'END {print(NR)}' Output unique list of available sections under which to create a DEB package.
awk '!A[$1]++ {print($1)}' <<< "$(dpkg-query --show -f='${Section}\n')" Using process substitution (`<()` is NOT command substitution), with AWK and its associative array variables, we can print just column 2 for lines whose first column is equal to what's between the double-quotes.
awk '{NR != 1 && A[$1]=$2} END {print(A["Mem:"])}' <(free -h) While below is an easier and simpler solution to the above, it's not at all the same, and in other cases, the above is definitely preferable.
awk '/^Mem:/ {print($2)}' <(free -h) Output list of unique uppercase-only, sigil-omitted variables used in FILE.
awk ' cheat.sheets: awk
{ cheat.sheets: awk
for(F=0; F<NF; F++){ cheat.sheets: awk
if($F~/^\$[A-Z_]+$/){ cheat.sheets: awk
A[$F]++ cheat.sheets: awk
} cheat.sheets: awk
} cheat.sheets: awk
} cheat.sheets: awk
END { cheat.sheets: awk
for(I in A){ cheat.sheets: awk
X=substr(I, 2, length(I)) cheat.sheets: awk
printf("%s\n", X) cheat.sheets: awk
} cheat.sheets: awk
} cheat.sheets: awk
' FILE Output only lines from FILE between PATTERN_1 and PATTERN_2. Good for logs.
awk '/PATTERN_1/,/PATTERN_2/ {print}' FILE Pretty-print a table of an overview of the non-system users on the system.
awk -F ':' ' cheat.sheets: awk
BEGIN { cheat.sheets: awk
printf("%-17s %-4s %-4s %-s\n", "NAME", "UID", "GID", "SHELL") cheat.sheets: awk
} cheat.sheets: awk
$3 >= 1000 && $1 != "nobody" { cheat.sheets: awk
printf("%-17s %-d %-d %-s\n", $1, $3, $4, $7) cheat.sheets: awk
} cheat.sheets: awk
' /etc/passwd Display the total amount of MiB of RAM available in the machine. This is also a painful but useful workaround to get the units comma-separated, as would be doable with Bash's own `printf` built-in.
awk '/^MemTotal:/ {printf("%'"'"'dMiB\n", $2 / 1024)}' It's possible to sort strings in AWK, as well as uniq-ing, meaning you can replace uniq(1) and sort(1) calls with just the one call of AWK. Granted, you can use `sort -u` to do both, but AWK offers much more functionality. Unlike when using AWK to uniq-ify, uniq(1) only works by adjacency, meaning the duplicate lines must be adjacent to one another for them to be handled.
awk ' cheat.sheets: awk
{ cheat.sheets: awk
!Lines[$0]++ cheat.sheets: awk
} cheat.sheets: awk
END { cheat.sheets: awk
asorti(Lines, Sorted) cheat.sheets: awk
for (Line in Sorted) { cheat.sheets: awk
print(Sorted[Line]) cheat.sheets: awk
} cheat.sheets: awk
} cheat.sheets: awk
' FILE Remove duplicate lines
awk '!seen[$0]++' file.txt Remove all empty lines
awk 'NF > 0' file.txt Awk is a powerful text processing language designed for field-oriented data extraction, reporting, and transformation. It's ideal for processing structured text files like logs, CSVs, and tables.
Awk programs follow the pattern: pattern { action }
| Variable | Meaning |
|---|---|
$0 |
Entire current line |
$1, $2, ... |
First, second, ... field |
NF |
Number of fields in current record |
NR |
Current record number (line number) |
FS |
Field separator (default: whitespace) |
OFS |
Output field separator |
# Print columns 1, 3, and last
awk '{print $1, $3, $NF}' file.txt
# With header
awk 'BEGIN {print "Name\tValue"} {print $1, $2}' data.txt
# Custom separator
awk -F: '{print $1, $3}' /etc/passwd
# Lines where column 3 > 100
awk '$3 > 100' data.txt
# Lines matching regex
awk '/error/' log.txt
# Combined conditions
awk '$1 ~ /^192\.168/ && $3 > 1000' access.log
# Sum column
awk '{sum += $2} END {print sum}' data.txt
# Average
awk '{sum += $2; count++} END {print sum/count}' data.txt
# Group by category
awk '{count[$1]++} END {for (k in count) print k, count[k]}' data.txt