"-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
BEGIN
statement would be the most correct (being consistent with the overall awk
syntax).
BEGIN
if I use a file to store the whole thing, while -F
comes in handy with one-liners.
awk 'BEGIN{print split("foo:bar",a)}' FS=":" file
and awk 'BEGIN{FS=":"; print split("foo:bar",a)}' file
-F
is an argument to awk
itself:
$echo "1: " | awk -F":" '/1/ {print $1}'
1
You can also use a regular expression as a field separator. The following will print "bar" by using a regular expression to set the number "10" as a separator.
echo "foo 10 bar" | awk -F'[0-9][0-9]' '{print $2}'
Or you can use:
echo "1: " | awk '/1/{print $1-":"}'
This is a really funny equation.
/1/
mean?
There isn't any need to write this much. Just put your desired field separator with the -F
option in the AWK command and the column number you want to print segregated as per your mentioned field separator.
echo "1: " | awk -F: '{print $1}'
1
echo "1#2" | awk -F# '{print $1}'
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...
awk
ignored it - awk
reads that as one regex's boolean outcome ( 1 / 0 ) , then numerically minus
a variable named F
, then string concat with a single colon (:
), which means the total pattern yielded true because it's a non-empty string, thus $1
split by default space gets printed
echo "1: " | "456:abc:515:xyz "
awk -F: NF=/1/
1 | 456
UPDATE : realizing how verbose my previous answer was
Success story sharing
(pattern){action}
. Ifpattern
(mostly a conditional statement) is true,action
is executed. Ifpattern
is not available,true
is implied. Here thepattern
is/1/
which states is regex1
matched in the current record$0