awk
Quick Reference
Command Name:
awk
Category:
text processing
Platform:
linux
Basic Usage:
Common Use Cases
- 1
Text processing
Manipulate text data and extract information
- 2
Data analysis
Perform complex data analysis and transformations
- 3
Scripting
Use in shell scripts to process text data programmatically
- 4
Log analysis
Analyze log files and extract relevant information
Syntax
awk [options] 'program' file... awk [options] -f program-file file...
Options
Option | Description |
---|---|
-F fs | Set field separator to fs (a single character or regular expression) |
-f program-file | Read awk program from a file instead of from the command line |
-v var=value | Assign value to variable var before program execution begins |
-W option | Implementation-specific options (such as compatibility modes) |
-m[fr] val | Set various memory limits to val |
-b | Treat the input data as binary (requires gawk) |
-d[file] | Dump variables to file for debugging |
-D[file] | Debug mode - useful for development of complex awk programs |
-e 'program-text' | Program text is source code - allows multiple -e options |
-E file | Equivalent to -f file (POSIX only) |
-g | Enable GNU extensions (only affects --traditional) |
-o[file] | Write profiling information to file (gawk) |
-p[file] | Enable profiling and write profile to file (gawk) |
-P | Enable POSIX compatibility |
-r | Consider source program to be raw text, not awk program |
-V | Print version information |
Examples
How to Use These Examples
The examples below show common ways to use the awk
command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.
Basic Examples:
awk '{print $1}' file.txt
Prints the first field (column) of each line in file.txt. By default, awk splits input lines on whitespace, treating each resulting part as a field accessed via $1, $2, etc.
awk -F, '{print $1, $3}' data.csv
Processes a CSV file, printing the first and third columns. The -F option sets the field separator to a comma, making it suitable for CSV processing.
awk '/error/ {print $0}' logfile.txt
Prints lines containing the pattern "error" in logfile.txt. The pattern between slashes is a regular expression that finds matching lines, and $0 represents the entire line.
awk '{sum += $1} END {print "Sum:", sum}' numbers.txt
Calculates the sum of values in the first column of numbers.txt. The program accumulates values in the "sum" variable and prints the result after processing all lines.
Advanced Examples:
awk 'BEGIN {FS=","} {if ($3 > 1000) print $1, $3}' sales.csv
Prints the name and amount from a CSV file for transactions over $1000. The BEGIN block sets the field separator, and the conditional statement filters records based on the value in the third column.
awk '{count[$1]++} END {for (word in count) print word, count[word]}' textfile.txt
Counts word frequency in a text file. This uses an associative array to track occurrences of each word, then outputs each unique word with its count.