Text processing

Linux awk command

Process text by fields and patterns; fields are separated by whitespace by default.

Availability: Common Linux commands; shell built-ins and external implementations may differ by distribution.中文

Syntax

awk [OPTION]... 'PROGRAM' [FILE]...

Examples

Print the first colon-separated field.

awk -F: '{print $1}' /etc/passwd

Print the first and third fields where the third exceeds 100.

awk '$3 > 100 {print $1, $3}' scores.txt

Parameters, options and clauses

Option / clauseDescription
-F FS, --field-separator=FSSet the input field separator.
-v VAR=VALUEAssign a variable before the program runs.
-f FILERead the awk program from FILE.
-e PROGRAMAdd program source text.
-i FILELoad an awk extension library (gawk).
Safety note

Review a command and its target before running it. Commands that alter files, services, accounts, disks, permissions or databases may be difficult to reverse. Test in a safe environment and keep backups.

Official reference ↗Back to directory