find & replace comma in a .csv file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find & replace comma in a .csv file.
# 1  
Old 11-17-2009
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","lastname2,firstname2","xxxxxx","12/10/2009","16/10/2009","0","N","Leave"
"3","lastname3,firstname3","xxxxxx","29/10/2009","30/10/2009","0","N","Leave"

thanks.
# 2  
Old 11-17-2009
By sed:

Code:
sed 's/\(lastname[0-9]\),/\1;/' urfile
"1","lastname1;firstname1","xxxxxx","19/10/2009","23/10/2009","0","N","Leave"
"2","lastname2;firstname2","xxxxxx","12/10/2009","16/10/2009","0","N","Leave"
"3","lastname3;firstname3","xxxxxx","29/10/2009","30/10/2009","0","N","Leave"

By awk

Code:
awk 'BEGIN {FS=OFS=","} {print $1,$2";"$3,$4,$5,$6,$7,$8,$9}' urfile
"1","lastname1;firstname1","xxxxxx","19/10/2009","23/10/2009","0","N","Leave"
"2","lastname2;firstname2","xxxxxx","12/10/2009","16/10/2009","0","N","Leave"
"3","lastname3;firstname3","xxxxxx","29/10/2009","30/10/2009","0","N","Leave"

# 3  
Old 11-17-2009
Quote:
Originally Posted by libin4u2000
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","lastname2,firstname2","xxxxxx","12/10/2009","16/10/2009","0","N","Leave"
"3","lastname3,firstname3","xxxxxx","29/10/2009","30/10/2009","0","N","Leave"

thanks.
Another way:

Code:
awk -F "\"" '{sub(",",";",$4)}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. 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

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

4. Shell Programming and Scripting

Find & replace --> create a new file

Hi All, I have a unix shell script file as below. My task is a)to replace 248 to 350 and need to create a new file as BW3_350.sh b)to replace 248 to 380 and need to create a new file as BW3_380.sh c)to replace 248 to 320 and need to create a new file as BW3_320.sh there is no... (6 Replies)
Discussion started by: karthi_mrkg
6 Replies

5. UNIX for Dummies Questions & Answers

Find & Replace command - Fasta file

Hi all ! I have a fasta file that looks like that: >Sequence1 RTYIPLCASQHKLCPITFLAVK (it's just an example, obviously in reality I have several pairs of lines like that) Using UNIX command(s), would it be possible to replace all the characters except the "C" of the second line only by... (7 Replies)
Discussion started by: Cevin21
7 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. UNIX for Advanced & Expert Users

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.... (6 Replies)
Discussion started by: eskay_s
6 Replies

8. Shell Programming and Scripting

Converting .xls into .csv and find & Replace

Hi All, Please give me the solution to the following ASAP. 1) Converting the .xls into .csv Script i tried, mv hello.xls hello.csv The above given script converting the .xls file into .csv successfully. But after i run the below unix command I am no able to open the .csv file, its giving... (4 Replies)
Discussion started by: velava
4 Replies

9. Shell Programming and Scripting

Find and replace a column that has '' to NULL in a comma delimited using awk or sed

Hi this is my first time posting ever. I'm relatively new in using AWK/SED, I've been trying many a solution. I'm trying to replace the 59th column in a file where if I encounter '' then I would like to replace it with the word NULL. example 0 , '' , '' , 0 , 195.538462 change it to 0... (5 Replies)
Discussion started by: gumal901
5 Replies

10. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies
Login or Register to Ask a Question