Open In App

gawk command in Linux with Examples

gawk command in Linux is used for pattern scanning and processing language. The awk command requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. It is a utility that enables programmers to write tiny and effective programs in the form of statements that define text patterns that are to be searched for, in a text document and the action that is to be taken when a match is found within a line. 

gawk command can be used to : 
 



Syntax: 
 

gawk [POSIX / GNU style options] -f progfile [--] file ...
gawk [POSIX / GNU style options] [--] 'program' file ...

Some Important Options: 



 

Examples: 

 

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

gawk -F: -f mobile.txt /etc/passwd

Some Built In Variables: 

 

Examples: 

 

gawk '{print NR "-" $1 }' mobile.txt

gawk 'BEGIN{FS=":"; RS="-"} {print $1, $6, $7}' /etc/passwd

gawk 'BEGIN{FS=":"; OFS="-"} {print $1, $6, $7}' /etc/passwd

Sample More Commands with Examples: 

 

cat > mobile.txt

gawk '{print}' mobile.txt

gawk '/Sunil/ {print}' mobile.txt 

gawk '{print $2}' mobile.txt

gawk '{print NR, $0}' mobile.txt

gawk '{ if (length($0) > max) max = length($0) } END { print max }' mobile.txt

gawk 'END { print NR }' mobile.txt

gawk 'length($0) > 5' mobile.txt

Note: 

 

man gawk
gawk --help

 

Article Tags :