Difference between revisions of "Awk"
From Linuxintro
imported>ThorstenStaerk |
imported>ThorstenStaerk |
||
(10 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | [[awk]] is a [[command]] for string operations. | + | [[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 | + | [[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 = | ||
+ | * [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