user173446
user173446

Reputation: 3969

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

Given the following command,

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

why does AWK output:

1:

?

Upvotes: 391

Views: 880817

Answers (9)

RARE Kpop Manifesto
RARE Kpop Manifesto

Reputation: 2809

echo "1: " | "456:abc:515:xyz "
awk -F: NF=/1/            
      1    |  456

UPDATE : realizing how verbose my previous answer was

Upvotes: -1

Jürgen Hötzel
Jürgen Hötzel

Reputation: 19717

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

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

Upvotes: 606

fedorqui
fedorqui

Reputation: 289485

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

Upvotes: 79

Vonton
Vonton

Reputation: 3324

Or you can use:

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

This is a really funny equation.

Upvotes: 5

Zlemini
Zlemini

Reputation: 4963

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

Upvotes: 13

jihed gasmi
jihed gasmi

Reputation: 299

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...

Upvotes: 5

Bhavuk Taneja
Bhavuk Taneja

Reputation: 49

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

Upvotes: 4

Dennis Williamson
Dennis Williamson

Reputation: 359845

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.

Upvotes: 86

danben
danben

Reputation: 83220

-F is an argument to awk itself:

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

Upvotes: 11

Related Questions