Contents
- INDEX Associated Information - none Useful Links |
Some useful awk scriptsAn example of a loop in awk
Uses a counter to loop the specified amount To print last field of a record when the number of fields is unkown awk 'BEGIN { FS = "/" } { print $NF }' In this example the field seperator (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
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 seperate, 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:
To print everything but the last character with variable length records
maths example awk '{ result = $1 / 1000000 printf("%.1fn", result) } prints the results to one decimal place sed examplesTo 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;} |