user1096734
user1096734

Reputation:

How to escape a single quote inside awk

I want do the following

awk 'BEGIN {FS=" ";} {printf "'%s' ", $1}'

But escaping single quote this way does not work

awk 'BEGIN {FS=" ";} {printf "\'%s\' ", $1}'

How to do this? Thanks for help.

Upvotes: 151

Views: 165952

Answers (9)

daywalkerLinux
daywalkerLinux

Reputation: 11

this is how to print a single quote in awk: "\047"

example:

for i in aA bB cC ; do echo $i | awk '{print "\047"$1"\047"}' ; done

'aA'
'bB'
'cC'

Upvotes: 1

Frauke
Frauke

Reputation: 1

Another way: awk 'BEGIN {FS=" ";} {printf "%c%s%c ", 39, $1, 39}'

Use %c and give the ASCII code number for single quotes, which is 39. By the way, if you need to print double quotes, the ASCII code is 34!

Upvotes: -1

mklement0
mklement0

Reputation: 437933

Another option is to pass the single quote as an awk variable:

awk -v q=\' 'BEGIN {FS=" ";} {printf "%s%s%s ", q, $1, q}'

Simple example with string concatenation:

# Prints 'test me', *including* the single quotes.
awk -v q=\' '{ print q $0 q }' <<<'test me'

Upvotes: 46

RARE Kpop Manifesto
RARE Kpop Manifesto

Reputation: 2801

UPDATE 1 : more hands free approach to the $1 single quoting problem :

 1  echo " 123  xyz 456 abc " 

    '123'

        
{m,g}awk 'BEGIN { __=_
                  OFS =  sprintf("%c",
                    ++_  +_++*_+_++^++_+_*++_)
            _+=_^= FS  = "^|[[:space:]]+"
      } NF +=_ ==( NF = _)'

===========================

mawk has, by far, the most concise syntax to wrap lines in single quotes without a printf statement :

gawk 'NF+=   substr(!_, $2 =$_ ($_=_))' FS='^$' OFS='\47'
nawk '$2 =$-_ substr(_, $-_=_, NF = 3)' FS='^$' OFS='\47'

mawk '$++NF=$_ ($_=_)' FS=^$ OFS=\\47

INPUT

<( [[[a]]]bc:
              뀿 123=)>

OUTPUT

' [[[a]]]bc:
             뀿 123='

I intentionally wrapped it in <(…)> to point out there's leading edge space. The gap in between the two lines is a \f : form feed \014.

That said, this only wraps lines without escaping single quotes within the line itself.

To use sub() instead, it'll look like :

{m,n,g}awk -F'^$' 'sub(".*","\47&\47")'

Upvotes: -1

Jeronimo Robles
Jeronimo Robles

Reputation: 41

When you are using awk in the command line, you don't have to use the BEGIN block to define the field separator (FS) you can only use -F" " like:

awk -F" " {printf "\047%s\047 ", $1}'

saves you some typing. :)

Upvotes: 1

user1708042
user1708042

Reputation: 1863

For small scripts, an optional way to make it readable is to use a variable like this:

awk -v fmt="'%s'\n" '{printf fmt, $1}'

I found it convenient in a case where I had to produce many times the single-quote character in the output and the \047 were making it totally unreadable.

Upvotes: 7

Sergio K
Sergio K

Reputation: 201

awk 'BEGIN {FS=" "} {printf "\047%s\047 ", $1}'

Upvotes: 20

Steve
Steve

Reputation: 54402

This maybe what you're looking for:

awk 'BEGIN {FS=" ";} {printf "'\''%s'\'' ", $1}'

That is, with '\'' you close the opening ', then print a literal ' by escaping it and finally open the ' again.

Upvotes: 221

tiagojco
tiagojco

Reputation: 1183

A single quote is represented using \x27

Like in

awk 'BEGIN {FS=" ";} {printf "\x27%s\x27 ", $1}'

Source

Upvotes: 104

Related Questions