sed add double quotes and comma

Hi,

00000119EEEC3F25    feedoor    20171103
0000011A4F152077    feedard    20171024
00000191FA295F61    feedzipperhola    20171023
00000213C57BB856    feedriodapple    20171005
0000025F778EF9D5    joobakoolrk    20171004

I needed the result as:

"00000119EEEC3F25",    "feedoor",    "20171103",
"0000011A4F152077",    "feedard",    "20171024",
"00000191FA295F61",    "feedzipperhola",    "20171023",
"00000213C57BB856",    "feedriodapple",    "20171005",
"0000025F778EF9D5",    "joobakoolrk",    "20171004",

I tried using

sed 's/^/"/g  file.txt
  • Just gets me " at the beginning only..

Can someone advise how do I get the needed result? Thanks!

Are those "gaps" <TAB> chars or multiple spaces? Should they be preserved as is?

Yes tabs and no need to preserve it.

Hello ashokvpp,

Could you please try following and let me know if this helps you.
Solution 1st: In case you don't want TAB as delimiter then following may help you.

awk '{for(i=1;i<=NF;i++){$i=s1 $i s1 s2}} 1' s1="\"" s2=","   Input_file

Solution 2nd: In case you need TAB as a output delimiter.

awk '{for(i=1;i<=NF;i++){$i=s1 $i s1 s2}} 1' s1="\"" s2=","  Input_file | column -t

Output will be as follows.

"00000119EEEC3F25",  "feedoor",         "20171103",
"0000011A4F152077",  "feedard",         "20171024",
"00000191FA295F61",  "feedzipperhola",  "20171023",
"00000213C57BB856",  "feedriodapple",   "20171005",
"0000025F778EF9D5",  "joobakoolrk",     "20171004",

Thanks,
R. Singh

1 Like

A sed one:

sed 's/       /",     "/g;s/^/"/;s/$/"/' file
1 Like

Be it as it may - try

sed 's/[[:space:]]\+/",&"/g; s/^/"/; s/$/",/' file
1 Like

Also try:

sed 's/[^[:blank:]]\{1,\}/"&",/' file

--
GNU sed:

sed 's/[^ \t]\+/"&",/g' file
1 Like

The * (match zero or more) is problematic here.
The proper replacement for \+ is \{1,\}

sed 's/[^[:blank:]]\{1,\}/"&",/g' file.txt

--
The following variant adds quotes and comma only if not yet present

sed '
  s/^\([^"]\)/"\1/
  s/\([^",[:blank:]]\)\([[:blank:]]\)/\1",\2/g
  s/\([[:blank:]]\)\([^"[:blank:]]\)/\1"\2/g
  s/\([^",]\)$/\1",/
' file.txt
2 Likes
perl -pe 's/(\w+)/"$1",/g' ashokvpp.file

Output:

"00000119EEEC3F25",    "feedoor",    "20171103",
"0000011A4F152077",    "feedard",    "20171024",
"00000191FA295F61",    "feedzipperhola",    "20171023",
"00000213C57BB856",    "feedriodapple",    "20171005",
"0000025F778EF9D5",    "joobakoolrk",    "20171004",
1 Like

You are right of course. That was a copy-paste error :frowning:

1 Like