awk
Text processing
Usage
awk '{print $1}' [file]Splits each line into fields and runs a little program over them; $0 is the whole line and $1 the first field, and the default separator is any run of whitespace, not one space.
Common flags
| Flag | Meaning |
|---|---|
| -F | Set the field separator, as in -F, |
| -v | Pass a shell value in as an awk variable |
| -f | Read the program from a file |
| $0 | The whole line; $1 is the first field, NF the field count |
Examples
awk -F, '{print $2}' data.csvSecond column of a comma-separated file.
awk '$3 > 100' access.logOnly the rows whose third field exceeds 100.
awk '{s+=$1} END {print s}' nums.txtAdds up the first column.
These were built to be piped together. Three joined commands are usually shorter than one that does everything.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does awk do?
Splits each line into fields and runs a little program over them; $0 is the whole line and $1 the first field, and the default separator is any run of whitespace, not one space.
Q. How do I type it?
awk '{print $1}' [file] — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
4 are listed here; the full set is in man awk. This command sits under Text processing.
Related commands
man page: man awk