Penguin

The Field Separator

It doesn't just have to white space between fields. In can be anything as long as you set the appropriate field separator. Eg:

  • echo "123:456:789" | awk -F ':' '{print $1,$2,$3,}'

The : after the -F tells us that : is the input field separator. Change this as appropriate.

Multiple Field Separators.

What if there are multiple characters you wish to use as field separators? After the -F just list all of the characters in square brackets. For example:

  • echo "123:456=789" | awk -F '[:=]' '{print $1,$2,$3}'

Using Bash Variables inside awk.

To use the value of a bash variable. You have to use the -v switch to import it. The syntax is:

  • awk -v awkvar=$bashvar {print awkvar}

Now you can use awkvar in your awk statements which will have the value of bashvar. Remember you CANNOT change the value of a bash variable using AWK.

Floating point output

you can use printf modifiers to specify the output of numbers, for example

printf "%.02f",a/b