How can I use ":" as an AWK field separator? How can I use ":" as an AWK field separator? bash bash

How can I use ":" as an AWK field separator?


"-F" is a command line argument, not AWK syntax. Try:

 echo "1: " | awk -F  ":" '/1/ {print $1}'


If you want to do it programatically, you can use the FS variable:

echo "1: " | awk 'BEGIN { FS=":" } /1/ { print $1 }'

Note that if you change it in the main loop rather than the BEGIN loop, it takes affect for the next line read in, since the current line has already been split.


You have multiple ways to set : as the separator:

awk -F: '{print $1}'awk -v FS=: '{print $1}'awk '{print $1}' FS=:awk 'BEGIN{FS=":"} {print $1}'

All of them are equivalent and will return 1 given a sample input "1:2:3":

$ awk -F: '{print $1}' <<< "1:2:3"1$ awk -v FS=: '{print $1}' <<< "1:2:3"1$ awk '{print $1}' FS=: <<< "1:2:3"1$ awk 'BEGIN{FS=":"} {print $1}' <<< "1:2:3"1