From RootdevWiki

Jump to: navigation, search

Contents

Awk Reference

Print Last Field

awk '{print $NF}' filename


Print Specific Lines from a File

awk 'NR == 2' filename
awk 'NR > 2 && NR < 10' filename

and so on....

One Liners

Get unique IPs from apache log and do a reverse lookup on them

for i in `awk '{print $1}' access_log|sort -u`; do host -va $i|grep -1 \ 
"ANSWER SECTION"| grep -v "ANSWER SECTION\|^$"|awk '{print $NF}'; done


Get non system account usernames from /etc/passwd

awk -F: '$3 >= 100 {print $1}' /etc/passwd


Turn a zone file into a reverse zone file

grep 10.4.1 * | awk 'split($4, x, /\./) split($1, y, /:/) \
{print x[4]"\tPTR\t"y[2]"."y[1]"."}'  >> ../reverse/1.4.10.in-addr.arpa

Print all lines from a file except lines that match a.* but not a.x

In this case, print all lines from an RPM repo comps.xml file except all the language lines other than those that match lang='en_GB'

awk '{ if ( $0 ~ /lang/ && $0 ~ /en_GB/ ) {print} else if \
( $0 !~ /lang/ ) {print}}' comps.xml > comps.xml.en

See Also

Bash#Behold_The_Power_of_Awk