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
awk -F, '{print $1, $3}' data.csv
awk '/error/ {print $0}' logfile.txt
awk '{sum += $1} END {print "Sum:", sum}' numbers.txt
awk -F: '{print $1, $3}' /etc/passwd