Linux command
awk 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Example
awk '{print $5}' [path/to/file]
Example
awk '/[foo]/ {print $2}' [path/to/file]
Example
awk -F ',' '{print $NF}' [path/to/file]
Sum the values
awk '{s+=$1} END {print s}' [path/to/file]
Example
awk '{if ($1 == "foo") print "Exact match foo"; else if ($1 ~ "bar") print "Partial match bar"; else print "Baz"}' [path/to/file]
Example
awk '($10 >= [min_value] && $10 <= [max_value])'
说明
awk is a pattern-scanning and text-processing language designed for extracting and transforming structured data. It reads input line by line, splits each line into fields, and applies user-defined rules consisting of patterns and actions. An awk program is a sequence of pattern { action } rules. For each input line, awk tests the patterns and executes the associated actions for any that match. If no pattern is given, the action applies to every line. If no action is given, matching lines are printed. Fields are accessed as $1, $2, etc., with $0 representing the entire line. The default field separator is whitespace, changeable with -F. Built-in variables include NR (current line number), NF (number of fields in current line), FS (field separator), and OFS (output field separator). Special patterns BEGIN and END execute actions before and after all input is processed, useful for initialization and summary output. Awk supports variables, arrays, arithmetic, string functions, printf formatting, and control flow statements, making it a complete programming language for text processing.
参数
- -F _fs_
- Field separator (_fs_); default whitespace or TAB
- -f _file_
- Read awk program from _file_ instead of command line
- -v _var_=_val_
- Assign _val_ to _var_ before program runs (repeatable)
- --
- End options; treat following as filenames
- -V
- Print version and exit (gawk)
- --help
- Print help and exit (gawk)
- --posix
- Enforce POSIX compatibility (gawk)
- -mf _n_
- Limit function args to _n_ (debugging; gawk)
- -mr _n_
- Limit record size to _n_ bytes (debugging; gawk)
- -W _traditional_
- Use original awk behavior (gawk)
FAQ
What is the awk command used for?
awk is a pattern-scanning and text-processing language designed for extracting and transforming structured data. It reads input line by line, splits each line into fields, and applies user-defined rules consisting of patterns and actions. An awk program is a sequence of pattern { action } rules. For each input line, awk tests the patterns and executes the associated actions for any that match. If no pattern is given, the action applies to every line. If no action is given, matching lines are printed. Fields are accessed as $1, $2, etc., with $0 representing the entire line. The default field separator is whitespace, changeable with -F. Built-in variables include NR (current line number), NF (number of fields in current line), FS (field separator), and OFS (output field separator). Special patterns BEGIN and END execute actions before and after all input is processed, useful for initialization and summary output. Awk supports variables, arrays, arithmetic, string functions, printf formatting, and control flow statements, making it a complete programming language for text processing.
How do I run a basic awk example?
Run `awk '{print $5}' [path/to/file]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -F _fs_ do in awk?
Field separator (_fs_); default whitespace or TAB