quoting just selected words

Hi all,

i have a file that looks like:

one:two:three:four:five
six:seven:eight:nine:ten

and i'd like to quote the fourth column, getting:

one:two:three:"four":five
six:seven:eight:"nine":ten

i was thinking something like:

awk 'BEGIN{FS=":"}{print $1 FS $2 FS $3 FS \"$4\" FS $5}'

but clearly i'm wrong and seems like really not efficient.
Any idea please?

thanks

D.

Try:

echo "one:two:three:four:five"| awk '$4="\""$4"\""' FS=':' OFS=':'

Hi, try with

awk 'BEGIN{FS=":"}{print $1 FS $2 FS $3 FS "\"" $4"\"" FS $5}' file_name

it's sure anybody will post a better solution...

---------- Post updated at 12:26 PM ---------- Previous update was at 12:19 PM ----------

Beautiful and elegant solution!

Hi

thanks boths

D.

awk -F: '$4="\""$4"\""' OFS=: infile
sed 's/\([^:]*\)/"\1"/4' infile
sed 's/:/&"/3;s/:/"&/4' infile

---------- Post updated at 12:58 ---------- Previous update was at 12:38 ----------

This one's a bit shorter:

sed 's/[^:]*/"&"/4' infile
1 Like

try

 awk -F: -v a='"' ' $4=a$4a ' OFS=: input_file