1753665 Members
5186 Online
108798 Solutions
New Discussion юеВ

Re: awk help

 
SOLVED
Go to solution
viseshu
Frequent Advisor

Re: awk help

Sandy,
Im generally asking this help... i need it in other script.Please help me out
Peter Nikitka
Honored Contributor

Re: awk help

Hi,

if you generally want to enclose fields with quotes and want to output all (valid) fields, modify my "Solution 2":
Solution 2b)
awk -F, -v qu='"' '{out=0; for(i=1;i<=NF;i++) {if(out) printf(FS);out++
if(! index($i,qu)) {printf("%s%s%s",qu,$i,qu); continue }
if (match($i,qu".*"qu) && (length($i)==RLENGTH)) printf($i)}
if(out) printf("\n")}' above.csv

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Peter Nikitka
Honored Contributor

Re: awk help

Hi,

reading your thread again: if you really don't care about the original placement of the quotes, you can use a 'forced' handling of your fields: drop all quotes at input and create new ones at output:

awk -F, -v qu='"' 'NF {gsub(qu,""); printf("%s%s%s",qu,$1,qu)
for(i=2;i<=NF;i++) printf("%s%s%s%s",FS,qu,$i,qu)
printf("\n")}' above.csv

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"