adding quotes around each column in a csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting adding quotes around each column in a csv file
# 1  
Old 01-06-2012
Power adding quotes around each column in a csv file

I saved the csv file in a comma delimited format.
Sample input
input.csv
1 abc 2
2 def 4
3 ghi 6
4 jkl 8
5 mno 10
output.csv should look like this with single quotes around each field
'1' 'abc' '2'
'2' 'def' '4'
'3' 'ghi' '6'
'4' 'jkl' '8'
'5' 'mno' '10'
Please help me Smilie
This is what I did. It does not work.
{linux1:~} cut -f1- -d"," 'input.csv'|sed -e 's/\([^ $]*\)/\"\1/g' -e 's/$/\"/' >output.csv Any help is appreciated Smilie
# 2  
Old 01-06-2012
Try this:-

Code:
 sed -e "s/^/'/" -e "s/ /' '/g" -e 's/$/'"'"'/' input.csv > output.csv

It puts a ' at the start and end of each line, and replaces all the spaces with ' '.

Last edited by Franklin52; 01-06-2012 at 06:03 PM..
# 3  
Old 01-06-2012
It does not work with
Code:
 sed -e "s/^/'/" -e "s/ /' '/g" -e 's/$/'"'"'/' input.csv > output.csv

The output looks something like this.
 
'1 abc 2
'
'2 def 4
'
'3 ghi 6
'
'4 jkl 8
'
'5 mno 10
'
# 4  
Old 01-06-2012
Code:
sed "s/[[:alnum:]]*/'&'/g" input >output

Code:
$ cat tst
1 abc 2
2 def 4
3 ghi 6
4 jkl 8
5 mno 10

Code:
$ sed "s/[[:alnum:]]*/'&'/g" tst
'1' 'abc' '2'
'2' 'def' '4'
'3' 'ghi' '6'
'4' 'jkl' '8'
'5' 'mno' '10'

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding new column data in csv from UNIX

Adding new column data in csv from UNIX Hi I need to add new column data daily to existing csv file. Please assist 7/11 7/10 7/9 7/8 space 10 GB 20 GB I was able to generate current day's data in csv but unable to add the previous 30 days data to the same csv Please use code tags,... (2 Replies)
Discussion started by: archana25
2 Replies

2. 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

3. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

4. Shell Programming and Scripting

How to delete the commas in a .CSV file that are enclosed in a string with double quotes?

Okay, I would like to delete all the commas in a .CSV file (TEST.CSV) or at least substitute them with empty space, that are enclosed in double quote. Please see the sample file as below: column 1,column 2,column 3,column 4,column 5,column 6,column 7,column 8,column 9,column 10... (8 Replies)
Discussion started by: dhruuv369
8 Replies

5. Shell Programming and Scripting

Adding a new column to a csv file

Hi, Can anyone please explain me how can I add a new column to a Csv file ? Actaully I am setting one set of commands and creating a CSV file with 4 columns. I am executing another command and it's output should be added as column 5 with a column heading ? Can anyone explain how can... (26 Replies)
Discussion started by: rrb2009
26 Replies

6. Shell Programming and Scripting

HELP with AWK or SED. Need to replace the commas between double quotes in CSV file

Hello experts, I need to validate a csv file which contains data like this: Sample.csv "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 I just need to check if all the records contain exactly the number of... (5 Replies)
Discussion started by: shell_boy23
5 Replies

7. 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

8. Shell Programming and Scripting

Fix CSV file with column missing quotes

I have a CSV file that is missing quotes around a column that contains text with commas. Example: Column1, Column2, Column3, Column4, Column5, Column6 Data1, Data2, Data3, Data, 4, Data5, Data6 Data1, Data3, Data3, Data, text, 4, Data5, Data6 I think the easiest way for me to fix this is to... (2 Replies)
Discussion started by: EmptyH
2 Replies

9. Shell Programming and Scripting

replace value with double quotes of specific coulmn value in csv file

Hi, I am trying to replace a specific column values in a csv file with double quotes. Example: SNO,NAME,ZIPCODE,RANK 1,Robert,74538,12 2,Sam,07564,13 3,Kim, Ed,12345,14 Desired Output: SNO,NAME,ZIPCODE,RANK 1,Robert Ken,74538,12 2,Sam Mik,"07564",13 3,"Kim, Ed",12345,14 I... (3 Replies)
Discussion started by: techmoris
3 Replies
Login or Register to Ask a Question