Difference between revisions of "Awk"

From Linuxintro
imported>ThorstenStaerk
 
Line 20: Line 20:
  
 
= See also =
 
= See also =
* [http://man-wiki.net/index.php/Awk gawk man page]
+
* [http://www.manpagez.com/man/1/awk/ gawk man page]
 
* [[piping]]
 
* [[piping]]
 
* [[Shell Scripting Tutorial]]
 
* [[Shell Scripting Tutorial]]

Latest revision as of 09:07, 23 April 2014

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