Linux command
gawk 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Print specific columns
gawk '{print $1, $3}' [path/to/file]
Use a different field separator
gawk -F':' '{print $1}' [/etc/passwd]
Sum values in a column
gawk '{sum += $1} END {print sum}' [file]
Filter lines matching a pattern
gawk '/pattern/ {print}' [file]
Print line numbers
gawk '{print NR": "$0}' [file]
Run an awk program from a file
gawk -f [program.awk] [file]
Use variables from command line
gawk -v name="[value]" '{print name, $0}' [file]
Print lines where column 3 > 100
gawk '$3 > 100 {print}' [file]
说明
gawk (GNU awk) is the GNU implementation of the AWK programming language, designed for text processing and data extraction. It processes input line by line, splitting each into fields that can be manipulated and transformed. An AWK program consists of patterns and actions: pattern { action }. When a line matches a pattern, the associated action executes. Special patterns BEGIN and END run before/after processing any input. Built-in variables include: $0 (entire line), $1-$n (fields), NR (record/line number), NF (number of fields), FS (field separator), OFS (output field separator), and RS (record separator). Gawk extends standard AWK with features like: network programming (TCP/UDP connections), time functions, bit manipulation, internationalization, and extension loading. It also supports regular expressions, arrays, user-defined functions, and getline for reading from files/pipes. Common uses include: log analysis, CSV/TSV processing, report generation, data transformation, and quick text manipulation tasks that would be verbose in other languages.
参数
- -F _fs_
- Set field separator (default: whitespace).
- -f _progfile_
- Read program from file instead of command line.
- -v _var=value_
- Assign value to variable before program execution.
- -b, --characters-as-bytes
- Treat all input data as single-byte characters.
- -c, --traditional
- Run in POSIX-compatible mode.
- -e _'program'_
- Specify program text (can use multiple -e options).
- -i _file_
- Include library file.
- -l _lib_
- Load extension library.
- -n, --non-decimal-data
- Recognize octal and hexadecimal values in input.
- -o _file_
- Pretty-print program to file.
- -p _file_
- Enable profiling, output to file.
- -S, --sandbox
- Disable system commands and file access.
- --help
- Display help information.
- --version
- Display version information.
FAQ
What is the gawk command used for?
gawk (GNU awk) is the GNU implementation of the AWK programming language, designed for text processing and data extraction. It processes input line by line, splitting each into fields that can be manipulated and transformed. An AWK program consists of patterns and actions: pattern { action }. When a line matches a pattern, the associated action executes. Special patterns BEGIN and END run before/after processing any input. Built-in variables include: $0 (entire line), $1-$n (fields), NR (record/line number), NF (number of fields), FS (field separator), OFS (output field separator), and RS (record separator). Gawk extends standard AWK with features like: network programming (TCP/UDP connections), time functions, bit manipulation, internationalization, and extension loading. It also supports regular expressions, arrays, user-defined functions, and getline for reading from files/pipes. Common uses include: log analysis, CSV/TSV processing, report generation, data transformation, and quick text manipulation tasks that would be verbose in other languages.
How do I run a basic gawk example?
Run `gawk '{print $1, $3}' [path/to/file]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -F _fs_ do in gawk?
Set field separator (default: whitespace).