Replacing the comma in .csv file in unix


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Replacing the comma in .csv file in unix
# 1  
Old 11-15-2010
Replacing the comma in .csv file in unix

Hi All,

Could some one help me on one of my requirement below:

I have a sequential file with 4fields in it and it is a comma (,) seperated file.

Delimeter is 'comma'.

But in of the file column for ex: 3rd column it is 'Description' (column name) I am getting the values with commas.

Since this file is comma delimeted file ,the comma mentioned in the 'Description' filed is also treating as a delimeter.

Now I need to replace this comma with 'Pipe' symbol in the data for the field 3 (i.e. 'Description')...

How can I do this in unix?

ex: I have a file called 'emp.csv'

Code:
empno,empname,description,salary
'1','sam','clerk,accounts','3000'
'2','Peter','Manager,accounts','5000'

In the above example I have total 4 fields in my file and it is comma seperated file.But in the 3rd filed(descrption) filed the value it self has the comma in it.

My requirement is to find the comma in 3rd field and replace them into pipe (|)

Please advise?

Thanks in advance

Last edited by radoulov; 11-15-2010 at 11:12 AM.. Reason: Code tags, please!
# 2  
Old 11-15-2010
Code:
perl -MText::ParseWords -nle'
  @cols = parse_line(",",0, $_);
  $cols[2] =~ s/,/|/;
  print join ",", @cols;
  ' infile

# 3  
Old 11-15-2010
Another way:
Code:
awk -F"'" '{sub(",","|",$6)}1' OFS=\' file

# 4  
Old 11-15-2010
Thanks for responding my query.

But I am not getting $6 is ?

My column position is 3...means I need to replace the commas with pipe in the 3rd field's data.....
# 5  
Old 11-15-2010
The discription column is the 6th field if the fieldseparator is a single quote:
Code:
cat file
'1','sam','clerk,accounts','3000'
'2','Peter','Manager,accounts','5000'
$ awk -F\' '{sub(",","|",$6)}1' OFS=\' file
'1','sam','clerk|accounts','3000'
'2','Peter','Manager|accounts','5000'

# 6  
Old 11-16-2010
Thanks Franklin52 for the reply..

I have one more qn on the same as what if the data has double quotes(") as a field seperator?
What could be the description column position ,if the field seperator is double quotes(") ?

Thanks for the patience...
# 7  
Old 11-16-2010
Quote:
Originally Posted by eskay_s
Thanks Franklin52 for the reply..

I have one more qn on the same as what if the data has double quotes(") as a field seperator?
What could be the description column position ,if the field seperator is double quotes(") ?

Thanks for the patience...
Just replace the field separators with a double quote:
Code:
awk -F\" '{sub(",","|",$6)}1' OFS=\" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Handling Comma in string values in a CSV file

Hi have a comma separated file which has numeric and string columns. String columns are quoted and can have comma in between the quotes. How to identify the columns with FS ="," sample records"prabhat,kumar",19,2000,"bangalore,India" In awk it should be$1 = prabhat,kumar $2=19 $3=2000... (9 Replies)
Discussion started by: prabhat.diwaker
9 Replies

2. UNIX for Dummies Questions & Answers

Add a field separator (comma) inside a line of a CSV file

Hi... I can't find my little red AWK book and it's been a long while since I've awk'd. But I need to take a CSV file and convert the first word of the fifth field to its own field by replacing a space with a comma. This is for importing a spreadsheet of issues into JIRA... Example: a line... (9 Replies)
Discussion started by: Tawpie
9 Replies

3. Shell Programming and Scripting

Pick the column value including comma from csv file using awk

Source 1 column1 column2 column 3 column4 1,ganesh,1,000,1 222,ram,2,000,5 222,ram,50,000,5 33,raju,5,000,7 33,raju,5,000,7 33,raju,5,000,8 33,raju,5,000,4 33,raju,5,000,1 In my .csv file, third column is having price value with comma (20,300), it has to be considered 1,000 as... (1 Reply)
Discussion started by: Ganesh L
1 Replies

4. Shell Programming and Scripting

Need Help - comma inside double quote in comma separated csv,

Hello there, I have a comma separated csv , and all the text field is wrapped by double quote. Issue is some text field contain comma as well inside double quote. so it is difficult to process. Input in the csv file is , 1,234,"abc,12,gh","GH234TY",34 I need output like below,... (8 Replies)
Discussion started by: Uttam Maji
8 Replies

5. Shell Programming and Scripting

Inserting additional comma delimiters in a csv file, after and before certian fields.

Hello I have a csv file which I need to insert addtional commas into. The csv is of the format field1,field2,field3,field4,...etc...,field13,field14 I need to add extra commas in each record so that the final output looks like ... (1 Reply)
Discussion started by: kamal_p_99
1 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

find & replace comma in a .csv file.

HI, Please find the text below. I receive a .csv file on server. I need the comma(,) in the second column to be replaced by a semi-colon( ; ). How to do it. Please help. Sample text: "1","lastname1,firstname1","xxxxxx","19/10/2009","23/10/2009","0","N","Leave"... (2 Replies)
Discussion started by: libin4u2000
2 Replies

8. Shell Programming and Scripting

Replacing strings in csv file.

Hi, I have a problem.. 1) I have a file that contains the lines as below : VRF-TM_DummyLab/mse02.lab,mse02.lab,ge-2/0/7.222 VRF-EMS_HUAWEI_MSAN_208/mse01.lab,mse01.lab,xe-1/0/0.208 2) I need a method to read this file, line by line from :... (5 Replies)
Discussion started by: msafwan82
5 Replies

9. UNIX for Advanced & Expert Users

How to load comma seperated values file (*.csv) into Oracle table

Hi all I need to input values in a .csv file into my Oracle table running in Unix, I wonder what would be the command to do so... The values are recorded in an excel file and I tried using a formatted text file to do so but failed because one of the field is simply too large to fit in the... (4 Replies)
Discussion started by: handynas
4 Replies

10. UNIX for Dummies Questions & Answers

How to load comma seperated values file (*.csv) into Oracle table

Hi all I need to input values in a .csv file into my Oracle table running in Unix, I wonder what would be the command to do so... The values are recorded in an excel file and I tried using a formatted text file to do so but failed because one of the field is simply too large to fit in the... (5 Replies)
Discussion started by: handynas
5 Replies
Login or Register to Ask a Question