How to override a single inverted comma in awk
command.
cat a.txt |awk '{print "Students name is '"$1"'"}'
I want output as below:
Students name is 'Rajeev'
You can use '"'"'
for each single quote that you want in the output.
The first quote ends the single quoted string. Then we start a double quoted string which simply expands to a single quote. Then we enter a new single quoted string for the rest of the command.
Since there are no spaces between the strings, bash will string them together. i.e. "a"'b'
is the same as "ab"
or 'ab'
You can do this:
echo ff | awk $'{print "Students name is \'" $1 "\'" }'
Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:
\\ backslash
\' single quote
\" double quote
\n new line
...
As explained here
To avoid escaping an other tricks you can use it's octal representation,as:
$ awk 'BEGIN{print "\047"}'
'
So:
$ awk '{print "Students name is \047"$1"\047"}' a.txt