LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Printing single quotes with awk (https://www.linuxquestions.org/questions/linux-newbie-8/printing-single-quotes-with-awk-894025/)

caspernoome 07-27-2011 04:27 AM

Printing single quotes with awk
 
Hi,

I have the following command, which works fine:

ll /backup/PROD |grep PROD_ |awk -F" " '{print "catalog backuppiece /backup/PROD/"$9";"}'

The output is:

catalog backuppiece /backup/PROD/PROD_29mi42vn_1_1;

BUT... I want single quotes in the result. One before /back.. and one before the ;
Like this:

catalog backuppiece '/backup/PROD/PROD_29mi42vn_1_1';

How do I do that? I tried using \' but it does not work.
The following command works, but I get an error message, which prevents me from using the command in a bash file:

ll /backup/PROD |grep PROD_ |awk -F" " '{print "catalog backuppiece \'\''/backup/PROD/"$9"\'\'';"}'
awk: warning: escape sequence `\'' treated as plain `''

Regards,

Casper

caspernoome 07-27-2011 06:12 AM

Hi,

I found a solution to this question - No need for any replies then.

I used awk -v

ls -l /backup/PROD |grep PROD_ |awk -F" " -v xx="'" '{print "catalog backuppiece " xx "/backup/PROD/"$9 xx";"}'

Output is:

catalog backuppiece '/backup/PROD/PROD_29mi42vn_1_1';


Regards,

Casper

MTK358 07-27-2011 07:08 AM

There is a much better way:

Code:

awk 'BEGIN { print "This is a single qoute: '\'' And this is, too: '"'"'" }'

grail 07-27-2011 10:30 AM

Or an even better idea:
Code:

awk '{print "catalog backuppiece \047"FILENAME"\047;";nextfile}' /backup/PROD/*PROD_*
The usual story here would also be not to parse ls as on my machine I only have 8 fields total so your $9 would fail for me.


All times are GMT -5. The time now is 06:28 PM.