Linux: Outputting single quotes in awk output

awk is a powerful utility for text processing, but the command itself is surrounded by single quotes and unfortunately does not allow you to escape single quotes within the output.

To workaround this, you can represent the single quote as its hexidecimal escape code “\x27”.

$ echo -e "name=Ben\nname=Jerry" | awk -F'=' '{ printf("insert into users (name) values (\x27%s\x27);\n",$2) }'

insert into users (name) values ('Ben');
insert into users (name) values ('Jerry');

It is surprising that you cannot escape a single quote with a backslash or doubling up the single quote, but at least there is a workaround.

REFERENCES

stackoverflow, how to escape a single quote inside awk

awk man page