Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
    
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

The field separator is set explicitly using

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

or

awk 'BEGIN{ FS=" "; } { print $2; }'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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