Quoting with awk

Getting awk to print certain special characters (e.g. the single quote ') can be cumbersome. This is frustrating especially when you want to generate SQL statements, where the single quote is used as the string delimiter.

One way to overcome this is to use escape sequences with (octal) ASCII codes:

awk '{print "\047" $1 "\047,\047" $2 "\047";}'

This will convert a list like

Doe John
Mustermann Max

into

'Doe','John'
'Mustermann','Max'


UPDATE
Alternatively, you can use \x with hexadecimal ASCII codes:

awk '{print "\x27" $1 "\x27,\x27" $2 "\x27";}'


136 Words

2013-08-23T03:24:00-07:00