Please see this


Contents

- INDEX
- Unix Scripts Index
- HP-UX
- Solaris
- Linux Index
- General Unix
- Unix Networking
- OtherUnix types
- Unix databases

Associated Information

- none

Useful Links

- GAWK Manual
- Sed resources

Some useful awk scripts

An example of a loop in awk

i = 1 while ( i <= 2 ) { print $i ++i }

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

VAR1=`cat $RECORD | \ awk 'BEGIN { FS=":" } { for (i=2;i<NF;i++) print $1} | 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 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:

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) }'

maths example

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

prints the results to one decimal place

sed examples

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;}


Copyright 2000 Intronet Computers Ltd
Email: Intronet Computers for enquiries