Operating System - HP-UX
1834394 Members
1806 Online
110066 Solutions
New Discussion

Re: printing/escaping comma's in awk

 
SOLVED
Go to solution
u856100
Frequent Advisor

printing/escaping comma's in awk

peoples,

I have the following (myfile.txt) :

1234567
abcdefg
2354324
3242323
8798799

I want to change this to :

'1234567',
'abcdefg',
'2354324',
'3242323',
'8798799',

hence wrapping the content for use with a SQL query.

I have tried the following :

cat myfile.txt | awk '{print("\'"$0"\'")}' myfile.txt > newfile.txt

cat myfile.txt | awl 'print{("\'%s\',",$1)}' myfile.txt > newfile.txt

but I am not getting any joy!

any suggestions would be appreciated thanks

John

chicken or egg first?
6 REPLIES 6
John Palmer
Honored Contributor
Solution

Re: printing/escaping comma's in awk

John,

How about using sed instead...

sed -e s/^/\'/ -e s/$/\'/ myfile.txt > newfile.txt

Regards,
John
monasingh_1
Trusted Contributor

Re: printing/escaping comma's in awk

you can also use awk -v var="'" and while printing use '{print var $1 var}'

do a man on awk...

hope this helps...
T G Manikandan
Honored Contributor

Re: printing/escaping comma's in awk

#vi /tmp/b
'1234567',
'abcdefg',
'2354324',
'3242323',
'8798799',


#cat /tmp/b|awk -F "'" '{print $2}'
monasingh_1
Trusted Contributor

Re: printing/escaping comma's in awk

cat mytext.txt |awk -v var1="'" -v var2="," '{print var1 $1 var1 var2}' > newfile
should work...

Robin Wakefield
Honored Contributor

Re: printing/escaping comma's in awk

Hi John,

you can use a single sed command:

sed "s/.*/'&',/" myfile.txt

rgds, Robin
Balaji N
Honored Contributor

Re: printing/escaping comma's in awk

how abt this. here x27 is the ascii key for "'".

hth
-balaji


$ cat a.txt
1234567
abcdefg
2354324
3242323
8798799
$ awk '{printf "\x27%s\x127 \n",$1 }' a.txt
'1234567'
'abcdefg'
'2354324'
'3242323'
'8798799'
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.