sed add double quotes and comma


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed add double quotes and comma
# 1  
Old 10-24-2017
sed add double quotes and comma

Hi,

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

I needed the result as:
Code:
"00000119EEEC3F25",    "feedoor",    "20171103",
"0000011A4F152077",    "feedard",    "20171024",
"00000191FA295F61",    "feedzipperhola",    "20171023",
"00000213C57BB856",    "feedriodapple",    "20171005",
"0000025F778EF9D5",    "joobakoolrk",    "20171004",

I tried using
Code:
sed 's/^/"/g  file.txt

- Just gets me " at the beginning only..

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


Moderator's Comments:
Mod Comment Please use CODE tags correctly as required by forum rules!

Last edited by RudiC; 10-24-2017 at 02:12 PM.. Reason: Added CODE tags.
# 2  
Old 10-24-2017
Are those "gaps" <TAB> chars or multiple spaces? Should they be preserved as is?
# 3  
Old 10-24-2017
Yes tabs and no need to preserve it.
# 4  
Old 10-24-2017
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.
Code:
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.
Code:
awk '{for(i=1;i<=NF;i++){$i=s1 $i s1 s2}} 1' s1="\"" s2=","  Input_file | column -t

Output will be as follows.
Code:
"00000119EEEC3F25",  "feedoor",         "20171103",
"0000011A4F152077",  "feedard",         "20171024",
"00000191FA295F61",  "feedzipperhola",  "20171023",
"00000213C57BB856",  "feedriodapple",   "20171005",
"0000025F778EF9D5",  "joobakoolrk",     "20171004",

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 10-24-2017
A sed one:
Code:
sed 's/       /",     "/g;s/^/"/;s/$/"/' file

This User Gave Thanks to Scott For This Post:
# 6  
Old 10-24-2017
Be it as it may - try
Code:
sed 's/[[:space:]]\+/",&"/g; s/^/"/; s/$/",/' file

This User Gave Thanks to RudiC For This Post:
# 7  
Old 10-24-2017
Also try:
Code:
sed 's/[^[:blank:]]\{1,\}/"&",/' file


--
GNU sed:
Code:
sed 's/[^ \t]\+/"&",/g' file


Last edited by Scrutinizer; 10-24-2017 at 11:17 PM.. Reason: Corrected copy-paste error with regards to the first suggestion. Thanks MadeInGermany...
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

2. Shell Programming and Scripting

Removal of comma within double quotes

Hi All, I am getting .csv file whenever there is a comma present between a field that field get enclosed with double quotes For eg as below abc,123,xxyy,2178 fgh,123,"x,x"yy",2178 ghi,123,"x,xyy",2178 jkl,123,xx"yy,2178 whereas I want my data as per below abc,123,xxyy,2178... (1 Reply)
Discussion started by: H_bansal
1 Replies

3. Shell Programming and Scripting

Replace Double quotes within double quotes in a column with space while loading a CSV file

Hi All, I'm unable to load the data using sql loader where there are double quotes within the double quotes As these are optionally enclosed by double quotes. Sample Data : "221100",138.00,"D","0019/1477","44012075","49938","49938/15043000","Television - 22" Refurbished - Airwave","Supply... (6 Replies)
Discussion started by: mlavanya
6 Replies

4. Shell Programming and Scripting

awk print - fields separated with comma's need to ignore inbetween double quotes

I am trying to re-format a .csv file using awk. I have 6 fields in the .csv file. Some of the fields are enclosed in double quotes and contain comma's inside the quotes. awk is breaking this into multiple fields. Sample lines from the .csv file: Device Name,Personnel,Date,Solution... (1 Reply)
Discussion started by: jxrst
1 Replies

5. Shell Programming and Scripting

Replace double double quotes using AWK/SED

Hi, I have data as "01/22/97-"aaaaaaaaaaaaaaaaa""aaa""aabbbbbbbbcccccc""zbcd""dddddddddeeeeeeeeefffffff" I want to remove only the Consequitive double quotes and not the one which occurs single. My O/P must be ... (2 Replies)
Discussion started by: Bhuvaneswari
2 Replies

6. Shell Programming and Scripting

Replacing comma with in double quotes in a csv file

Hello, I need to read a csv file and I am trying to replace a comma with a text DSEE?DSEE. Example Input "Chapter","NewTrains, "oldTrains","Delayed",10,"London" "Chapter","Newbuses,oldbuses","On Time",20,"London" Output "Chapter","NewTrainsDSEE?DSEE... (5 Replies)
Discussion started by: venkatvani
5 Replies

7. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

8. Shell Programming and Scripting

Removal of comma(,) present inbetween double quotes(" ")

Hi Experts, I have a file with some of the records contain double quotes. If I found a double quote(") in any particular record , I need to look for the next double quote in that particular record and in between these quotes, if any comma(,) is there I need to replace with Tilde (~) in the same... (12 Replies)
Discussion started by: vsairam
12 Replies

9. Shell Programming and Scripting

To Replace comma with Pipe inside double quotes

Hi, I have a requirement to replace the comma's inside the double quotes. The comma's inside the double quotes will get changed dynamically. Input Record: "Washington, DC,Prabhu,aju",New York Output Record: "Washington| DC|Prabhu|aju",New York I tried with the below command but it... (3 Replies)
Discussion started by: prabhutkl
3 Replies

10. Shell Programming and Scripting

sed removing comma inside double quotes

I have a csv file with lines like the followings 123456,"ABC CO., LTD","XXX" 789012,"DEF LIMITED", "XXX" before I bcp this file to database, the comma in "CO.," need to be removed first. My script is cat <filename> | sed 's/"CO.,"/"CO."/g' but it doesn't work. Can anyone here able to... (2 Replies)
Discussion started by: joanneho
2 Replies
Login or Register to Ask a Question