An example of a loop in awk

i = 1 
while ( i <= 2 ) 
  print $i 

Uses a counter to loop the specified amount To print last field of a record when the number of fields is unknown

awk 'BEGIN { FS = "/" } { print $NF }'

In this example the field separator (FS) is set to /. NF is equivalent to the number of fields. The last field will be the same number as the number of fields, therefore the last field will be printed. To print everything but the last field when the total number of fields is unknown

VAR1=`cat $RECORD | \ 
       awk 'BEGIN { FS=":" } { for (i=2;i<NF;i++) print $i}' | tr -d '[:space:]'`

The field separator is set to : . A loop is then performed which will print each field in turn, starting at field 2, looping while the number is less than the number of fields, i.e. one less than NF so the last field isn't printed. As each field is separate, tr can be used to “glue” them back together. Variable $VAR1 will then equal the sting minus the last record. An alternative method of doing this is:

awk '{ print substr($0,1,length($0) - length($NF)) }'

To print everything but the last character with variable length records

awk '{ print substr($0,1,length($0)-1) }'

Print the first field unless it starts with a #

cat file | awk 'BEGIN { FS = ":" } $1 !~ /^#/ {print $1}'

Finding processes that have a parent process of 1

ps -ef | awk '$3 ~ /^1$/ { print $0 }'

Splitting records

{ split($9,elements,"/") 
   if (elements[3] !~ /rdsk/ && elements[4] ~ /s2/ print $9 }

So if field $9 is a list of disk devices, only /dev/dsk/c*t*d*s2 would be matched. maths example

awk '{ result = $1 / 1000000 printf("%.1fn", result) }

prints the results to one decimal place Working out the space used by files

find /home -name "*.log" | xargs ls -ld | awk '{ result = result + $5 } END { print result }'

Printing a range of fields, in this example fields 1 and 2 won't get printed but the rest will

hammer host errata list --host myserver | awk ' $7 ~ /Critical/ || $7 ~ /Important/ { $1=$2="";  print $0 }'

Inserting a shell variable into awk

hammer --csv host errata list --host $SERVER | awk -v var=$SERVER 'BEGIN { FS = "," } $4 ~ /Critical/ { print var","$2","$3","$4 }'

Print fields separated by commas

cat myfile.csv | awk 'BEGIN { FS = ","; OFS=","} {print $4,$5}'

Print everything but the first field

echo "one two three" | awk '{first = $1; $1 = ""; print $0 }'

To print lines 10 15

sed -n '10,15p' file

To insert a line after match

/REGEXP/{x;s/^/line to insert/;x;G;}

To insert a line before match

/REGEXP/{x;s/^/line to insert/;G;}

To join 2 lines (useful if output is split over 2 lines)

 sed 'N;s/\n/ /'

To delete all lines from the line with the matched string to the end of a file

 sed '/pattern to match/,$ d'

grep also has some handy arguments

  • -m1 stop after pattern matched once
  • -A1 print one line after the pattern has been matched (useful for files where any entry spans multiple lines)


 tac alogfile | grep -m1 -A1 "match this" 

tac is reverse cat , i.e. concatenate starting at the end of the file , stop after one match of “match this” and print the next line (which as this is a reverse cat, is the line before the matched pattern)

Recent Changes

Contribute to this wiki

Why not help others by sharing your knowledge? Contribute something to this wiki and join out hall of fame!
Contact us for a user name and password