1

Here is the sample output:

   $echo "0.0.0.0 : 0.0.0.0" | awk -F':' '{print $1==$2}' 
0

Why are the strings not equal? What do I need to do to make awk think

duli
  • 1,621
  • 3
  • 16
  • 25

1 Answers1

5

This happens because you are using : as the delimiter. $1 will contain 0.0.0.0<space> and $2 will contain <space>0.0.0.0

You can specify a sequence of characters as delimiter:

... | awk -F' : ' '{print $1==$2}'

The above command is using the sequence: space colon space as delimiter.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Good one! You can also leave the FS as is and check 1st and 3rd fields: `awk '{print $1==$3}' `. – fedorqui Aug 21 '15 at 15:43
  • @fedorqui You are right! Doing so would be much simpler! (facepalm! :) – hek2mgl Aug 21 '15 at 15:48
  • Actually, `-F' : '` is not a regex, but a literal string. For what it's worth, the solution is not portable, because `-F` does not accept regex, but happens to work for the given input (see POSIX description of [awk](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html)). – Thomas Dickey Aug 23 '15 at 10:50
  • @ThomasDickey You are right, only `gawk` supports a regex as delimiter. While fixed patterns are a subset of a regexes (I had gawk in mind), I admit that *regex* was not the quite right term. Good catch! Changed that. – hek2mgl Aug 23 '15 at 11:02
  • Actually, [mawk](http://invisible-island.net/mawk/manpage/mawk.html) does this as well. I do not recall if BWK does. See also [default field separator for awk](http://stackoverflow.com/questions/30405694/default-field-separator-for-awk) – Thomas Dickey Aug 23 '15 at 13:08