awk Linux Command
Powerful text processing language for pattern scanning and data extraction.
What does awk do?
Powerful text processing language for pattern scanning and data extraction.
Syntax
awk 'pattern { action }' filePractical example
awk '{print $1, $3}' data.txtQuestions about awk
How do I print specific columns with awk?
Use `awk '{print $1, $3}' file` to print the first and third columns. Columns are separated by whitespace by default.
How do I change the field separator in awk?
Use `awk -F':' '{print $1}' /etc/passwd` to use colon as the field separator instead of whitespace.
How do I print the last column with awk?
Use `awk '{print $NF}' file` where NF is the number of fields. $NF gives the last field on each line.
How do I print all columns except some with awk?
Use `awk '{$1=""; print}' file` to remove the first column and print the rest. Or use `cut` with complementary fields.
How do I sum a column with awk?
Use `awk '{sum+=$1} END {print sum}' file` to sum the first column. The END block runs after processing all lines.
How do I filter lines with awk?
Use `awk '/pattern/ {print}' file` to print lines matching a pattern. Use `awk '$3 > 100 {print}' file` for numeric conditions.
How do I print line numbers with awk?
Use `awk '{print NR, $0}' file` to print the line number (NR) before each line.
How do I use awk with a command pipe?
Use `command | awk '{print $1}'` to process the output of another command. For example, `ps aux | awk '{print $1, $11}'`.
Related Search Commands
typeDisplays how a command name would be interpreted (alias, builtin, external, etc.).
sedStream editor for filtering and transforming text (find/replace, insert, delete).
whereisLocates the binary, source, and manual page files for a command.
sortSorts lines of text files alphabetically or numerically.
whichShows the full path of the executable that would run for a given command.
uniqReports or filters out repeated lines in a sorted file.