Which language do you prefer? English 简体中文 繁体中文
...

Part of Linux Commands Cheat Sheet

Syntax


awk options program file

From output (pipe):


echo 'Hello World' | awk options program

Examples

Demo


awk '{print "Hi"}' filename

File content:


line 1
line 2

Output:


Hi
Hi

Using Variables


echo 'Hello World' | awk '{print $1}'

$0 for the whole line.
$1 for the first field.
$n for the second field.

Output:


Hello

Specify Sperator


echo 'Me:Hello World' | awk -F: '{print $2}'

Space ' ' is the default sperator.

Output:


Hello World

Using Multiple Commands


echo 'Hello World' | awk '{$2="world"; print $0}'

Output:


Hello world

Reading Script From File


echo 'Hello World' awk -f filename

File content:


{
$2 = "world"
print $0
}

Preprocessing And Postprocessing


echo 'Hello World' | awk 'BEGIN {print "PREFIX"}{$2="world"; print $0}END {print "SUFFIX"}'

Output:


PREFIX
Hello world
SUFFIX

30 Examples For Awk Command In Text Processing - Like Geeks

wbswjc-blog