How to use awk separator of single quote

awkescape-charactersunix

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?

Best Answer

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}'

or

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}'
Related Question