Reset awk field separator

1

On my local system, it appears that the configuration for awk has somehow been changed.

Running the following command:

echo "Hi there" | awk '{print $2}'

On my machine this prints:

Hi there

And on my development server it prints:

there

I am sure that at some point my local machine would behave the same as my server - i.e. it should print only the second word.

How do I reset the awk field separator back to its default (which according to this page is a sequence of spaces)?

Alex Spurling

Posted 2011-05-12T16:04:11.723

Reputation: 2 632

Yeah, no... there's... there's no way it could possibly have the former output. Unless it's not awk. – Ignacio Vazquez-Abrams – 2011-05-12T16:06:56.313

Apologies. I was actually running this on my local system: echo "Hi there" | awk "{print $2}" Hadn't realised that double quotes and single quotes were used differently. – Alex Spurling – 2011-05-12T16:14:07.337

Indeed. Double quotes allow variable substitution, which would cause $2 to collapse to nothing, printing the whole line. – Ignacio Vazquez-Abrams – 2011-05-12T16:15:08.523

1You might have done this: echo Hi there | awk '{print $2}' – ceving – 2011-05-12T16:16:34.060

/ Ignacio: I hope one of you will post an answer regarding double quotes - then Alex can mark it as accepted. – RedGrittyBrick – 2011-05-12T19:31:05.703

Answers

1

The field separator is set explicitly using

awk -F' ' '{ print $2; }'

or

awk 'BEGIN{ FS=" "; } { print $2; }'

WeSee

Posted 2011-05-12T16:04:11.723

Reputation: 246