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)?

  • Yeah, no... there's... there's no way it could possibly have the former output. Unless it's not awk. – Ignacio Vazquez-Abrams May 12 '11 at 16:06
  • 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 May 12 '11 at 16:14
  • Indeed. Double quotes allow variable substitution, which would cause $2 to collapse to nothing, printing the whole line. – Ignacio Vazquez-Abrams May 12 '11 at 16:15
  • 1
    You might have done this: echo Hi there | awk '{print $2}' – ceving May 12 '11 at 16:16
  • / Ignacio: I hope one of you will post an answer regarding double quotes - then Alex can mark it as accepted. – RedGrittyBrick May 12 '11 at 19:31
1

The field separator is set explicitly using

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

or

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.