I have a file with 4 columns
a|b|c|d
I need to add single quotes around field 2 and 3
I need it to be like
a|'b'|'c'|d
I have a file with 4 columns
a|b|c|d
I need to add single quotes around field 2 and 3
I need it to be like
a|'b'|'c'|d
Any attempts / ideas / thoughts from your side, possibly fertilized by a search in these forums?
sed 's.|.'|.g' file
but this adding to the last field as well
I'm afraid that wouldn't run at all as there's a '
missing...
awk
OK? Try
awk -F\| -vSQ="'" '{$2 = SQ $2 SQ; $3 = SQ $3 SQ} 1' OFS=\| file
a|'b'|'c'|d
Hello dsravanam,
In case you have more than 4 fields then you could try following also(I haven't tested it though).
awk -F"|" -vs1="'" '{for(i=2;i<NF;i++){$i= s1 $i s1}} 1' OFS="|" Input_file
Thanks,
R. Singh
sed:
sed "s/[^|]\{1,\}/'&'/2; s/[^|]\{1,\}/'&'/3" file
or
sed "s/|\([^|]*\)|\([^|]*\)|/|'\1'|'\2'|/" file
GNU sed:
sed -r "s/[^|]+/'&'/2; s/[^|]+/'&'/3" file