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

I'd like to use awk to pull out information from SQL output like the following:

(count(distinct serverclass)='2')

And need to extract the number, which is 2 in this example.

I'd like to do something like the following, but cannot figure out how to escape the single quote:

<sqlCommand> | awk 'BEGIN{FS="'"}{print $2}'

How can I get a literal single quote as my field separator in awk?

share|improve this question
up vote 9 down vote accepted

To insert a single quote character in a single-quoted string, end the current string, write "'" or \' and begin the string again.

In your example, that's

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


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

However, using the -F switch to specify the field separator will result in more legible code:

awk -F\' '{print $2}'
share|improve this answer
Why did you update the style from -F"'" to -F\'? – WilliamKF Sep 6 '12 at 14:44
No good reason. Both work, but the first has too many quotes for my taste. – Dennis Sep 6 '12 at 14:47

Your Answer


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.