Score:3

How to correctly escape single quotes in AWK

mx flag

It isn't clear exactly what more needs to be escaped in the following macro to allow it to be used with awk or sed on FreeBSD.

define(`RELAY_MAILER_ARGS', `TCP $h 2525')dnl

Here is an awk command that attempts to insert the above line at line 90 below the SMART_HOST configuration.

awk 'NR==90 { print "define(\`RELAY_MAILER_ARGS\', \`TCP $h 2525\')dnl"}1' example.com.mc

The command results in the following error:

Unmatched '"'.

Note to future editors: the backtick/single quote is not a typo. This is an ancient quirk of the m4 language that is used to write the macros for Sendmail configuration: "m4 uses single quotes (opening "`" and closing "'") to quote arguments"

Additional Note: Octal escapes don't seem to work when using sed on FreeBSD. In that situation, use the xNN escaped hexadecimal characters in that situation.

Score:4
jp flag

You cannot escape single quotes as the command itself is surrounded by single quotes, but you could use an octal escape code \047 to represent ' in POSIX awk. Additionally, you could use a hexadecimal escape code \x27 in GNU awk (gawk).

From The GNU Awk User’s Guide, 3.2 Escape Sequences:

\nnn

The octal value nnn, where nnn stands for 1 to 3 digits between ‘0’ and ‘7’. For example, the code for the ASCII ESC (escape) character is \033.

\xhh…

The hexadecimal value hh, where hh stands for a sequence of hexadecimal digits (‘0’–‘9’, and either ‘A’–‘F’ or ‘a’–‘f’). A maximum of two digits are allowed after the \x. Any further hexadecimal digits are treated as simple letters or numbers. (c.e.) (The \x escape sequence is not allowed in POSIX awk.)

awk 'NR==90 { print "define(\`RELAY_MAILER_ARGS\047, \`TCP $h 2525\047)dnl"}1' example.com.mc

gawk 'NR==90 { print "define(\`RELAY_MAILER_ARGS\x27, \`TCP $h 2525\x27)dnl"}1' example.com.mc

If you would like to also use \140 (or \x60) to represent the backtick:

awk 'NR==90 { print "define(\140RELAY_MAILER_ARGS\047, \140TCP $h 2525\047)dnl"}1' example.com.mc

gawk 'NR==90 { print "define(\x60RELAY_MAILER_ARGS\x27, \x60TCP $h 2525\x27)dnl"}1' example.com.mc
mx flag
Thanks, there is a subtle trick with Sendmail configuration macros. The first "quote" character is a backtick and the second is a single quote. I understand what the correct answer should be now and thank you so much!! However, I would adjust your answer so that it looks like this: `\140RELAY_MAILER_ARGS\047, \140TCP $h 2525\047`.
jp flag
Thanks, I simply thought it was a typo. Updated my answer to cover this. Hope all the new examples are working as well.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.