Given the following command,
echo "1: " | awk '/1/ -F ":" {print $1}'
why does AWK output:
1:
?
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
AWK works as a text interpreter that goes linewise for the whole document and that goes fieldwise for each line. Thus $1, $2...$n are references to the fields of each line ($1 is the first field, $2 is the second field, and so on...).
You can define a field separator by using the "-F" switch under the command line or within two brackets with "FS=...".
Now consider the answer of Jürgen:
echo "1: " | awk -F ":" '/1/ {print $1}'
Above the field, boundaries are set by ":" so we have two fields $1 which is "1" and $2 which is the empty space. After comes the regular expression "/1/" that instructs the filter to output the first field only when the interpreter stumbles upon a line containing such an expression (I mean 1).
The output of the "echo" command is one line that contains "1", so the filter will work...
When dealing with the following example:
echo "1: " | awk '/1/ -F ":" {print $1}'
The syntax is messy and the interpreter chose to ignore the part F ":" and switches to the default field splitter which is the empty space, thus outputting "1:" as the first field and there will be not a second field!
The answer of Jürgen contains the good syntax...