Difference between revisions of "Awk"

From Linuxintro
imported>ThorstenStaerk
imported>ThorstenStaerk
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[awk]] is a [[command]] for string operations. For example, it allows you to show only the second ''column'' of a file. awk is not a simple command, but rather a programming language on its own. awk and gawk (for GNU awk) are used synonymously.
+
[[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:
 
;Examples:
Show only the second column of ''file.txt''
+
* Show only the second column of ''file.txt''
 
  awk '{print $2;}' ''file.txt''
 
  awk '{print $2;}' ''file.txt''
kill all processes of a user
+
* kill all processes of a user ''username''
  [[ps]] --no-header -fu <username> | [[awk]] '{ print $2 }' | [[grep]] -v $$ | [[xargs]] kill
+
  [[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[[piping||]][[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 =
 
= See also =
* [http://man-wiki.net/index.php/Awk gawk man page]
+
* [http://www.manpagez.com/man/1/awk/ gawk man page]
 +
* [[piping]]
 +
* [[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