1

I am unable to understand the answer given to it though certainly that helps to fulfil my requirement.

awk -F\| '{s="";for (i=1;i<=NF;i+=2) {s=s?s FS $i:$i} print s}' file

What does this {s=s?s FS $i:$i} print s} do? How does it print the required output? I know it is a ternary operator in use. But how does it evaluate the expression and print the output?

1

1 Answer 1

0

Think of it as a if-then-else

 {s=s?s FS $i:$i} 

evaluate to

 { if ( s != "" ) s=s FS $i ; ## i=3,5,7,...
   else s=$i ; } ## i=1

this looks like a obscure code.

awk -F\| '{s="";for (i=1;i<=NF;i+=2) {s=s?s FS $i:$i} print s}' file

is equivalent to (easiest to understand)

awk -F\| '{s=$1;for (i=3;i<=NF;i+=2) {s=s FS $i} print s}' file
1
  • Superb explanation man !!!
    – recmach
    Nov 11, 2014 at 13:17

Not the answer you're looking for? Browse other questions tagged .