Using this command, I can add "
before every line.
awk '{print "\""$0}' file
But I want to add '
instead of "
, why this command doesn't work?
awk '{print "\'"$0}' file
You absolutely CANNOT use a single quote inside a single-quote-delimited script as that quote absolutely denotes the end of that script [segment]. The options you have are:
{print "'"$0}
and execute as: awk -f foo file
, or\047
everywhere you want to print a quote: awk '{print "\047"$0}' file
, orawk -v q=\' '{print q$0}' file
, orawk '{print "'\''"$0}' file
The final list item above is actually 2 separate segments of awk script ('{print "'
and '"$0}'
) with a bit of shell in the middle (\'
) so when the end result is of concatenating those is passed by shell to the awk
command for execution, you get what you want.
FWIW I'd use "1" if your script is more than a couple of lines, "2" or "3" otherwise with "3" being my personal preference but YMMV. I wouldn't do 4 or 5.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With