Difference between revisions of "Awk"
From Linuxintro
Line 2: | Line 2: | ||
;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 |
[[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 | + | * extract ''eth0'''s MAC address from ifconfig output |
ifconfig | grep ''eth0'' | awk '{ print $5 }' | 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 = |
Revision as of 07:05, 27 September 2013
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
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