Awk

From Linuxintro
Revision as of 09:07, 23 April 2014 by imported>ThorstenStaerk (→‎See also)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

awk is a command for string operations. For example, it allows you to show only the second column of a file. awk and gawk (for GNU awk) are used synonymously.

Examples
  • Show only the second column of file.txt
awk '{print $2;}' file.txt
  • kill all processes of a user username
ps --no-header -fu username | awk '{ print $2 }' | grep -v $$ | xargs kill
  • extract eth0's MAC address from ifconfig output
ifconfig | grep eth0 | awk '{ print $5 }'
  • sorted list of memory consumption by applications
# ps aux|awk '{print $6"  " $11;s=s+$6} END {print s}' | sort -n

The output will look like this:

[...]
4028  dovecot/auth                                                                                                                                                    
4528  /sbin/haveged                                                                                                                                                   
8348  /usr/sbin/httpd2-prefork                                                                                                                                        
38764  /usr/sbin/httpd2-prefork                                                                                                                                                                                                                                 
43628  /usr/sbin/httpd2-prefork                                                                                                                                       
279148

See also