Parsing complicated CSV file with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing complicated CSV file with sed
# 1  
Old 05-19-2010
Parsing complicated CSV file with sed

Yes, there is a great doc out there that discusses parsing csv files with sed, and this topic has been covered before but not enough to answer my question (unix.com forums).

I'm trying to parse a CSV file that has optional quotes like the following:

Code:
"Apple","Apples, are fun",3.60,4.4,"I like eating apples, nuts and candies."
"Cucumber","How long is it?',6.0,3,"Pickles are cucumbers."

I'm about to give up and write my own program, whose algorithm would go something like this:

Code:
grab char
If char = "
  grab next char
  If char = comma remove
  else if char = " grab next char = , and end column 
  else echo char
else 
  If char = comma end column
  else echo char
  grab next char
end

Seems like it is a pretty simple algorithm, someone should be able to do this with a sed script?
# 2  
Old 05-19-2010
does it have to be done in 'sed'?
how 'bout this thread?

Last edited by vgersh99; 05-19-2010 at 08:10 PM..
# 3  
Old 05-20-2010
Assuming (from analyzing your test data) that only commas used within strings are followed by blanks, the following substitutes thus separates field separators from ordinary punctation marks:

Code:
[house@leonov] cat test.file | tr "," ";" | sed 's/; /, /g'
"Apple";"Apples, are fun";3.60;4.4;"I like eating apples, nuts and candies."
"Cucumber";"How long is it?';6.0;3;"Pickles are cucumbers."

If this were my script, however, I'd apply the above on a line by line basis, then count the fields of the resulting line to verify it being updated successfully ... How about single vs. double quotes, by the way?
# 4  
Old 05-20-2010
Not always a space following a comma, and single quotes are okay anywhere. I gave up and wrote it in PHP. Don't have awk installed, don't feel like installing it, plus with PHP I can do some other tasks for the procedure. This was just one step in a whole line of work that has to be done on the files.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Parsing a CSV File

Hello All, I have an input CSV file like below, where first row data can be in different position after every run of the tool, i.e. pzTest in below example is in column 1, but it can be also in 3 column and same for all the headers in the first row. pzTest, pzExtract, pxUpdate, pzInfo... (1 Reply)
Discussion started by: asirohi
1 Replies

2. Shell Programming and Scripting

Csv file parsing and validating

Hi, I have basic knowledge on unix shell scripting(not an expert). My requirement is reading the csv file using the schema defined in the configuration file and if the condition is not mached then move the unmatched record to a error file and matched good records into other file. In brief: ... (43 Replies)
Discussion started by: shree11
43 Replies

3. Shell Programming and Scripting

Parsing csv file and pass to a variable

Hi, Newbie here and I need some help to parse a csv file that contains fields separated by ",". What I need to achieve here is, read the 1 line file and extract 240 fields and pass to a variable and then read the next 240 fields and pass to a variable, over and over. If anyone can assist that... (4 Replies)
Discussion started by: tmslixx
4 Replies

4. Shell Programming and Scripting

Help required in parsing a csv file

Hi Members, I am stuck with the following problem. Request your kind help I have an csv file which contains, 1 header record, data records and 1 footer record. Sample is as below Contents of cm_update_file_101010.csv -------------------------------------------------- ... (6 Replies)
Discussion started by: ramakanth_burra
6 Replies

5. Shell Programming and Scripting

Parsing a CSV File

Hey guys, I'm in the process of learning PHP and BASH scripting. I'm getting there, slowly ;) I would like some help with parsing a CSV file. This file contains a list of hostnames, dates, and either Valid, Expired, or Expired Soon in the last column. Basically, I want to parse the file,... (12 Replies)
Discussion started by: dzl
12 Replies

6. Shell Programming and Scripting

2 problems: Mailing CSV file / parsing CSV for display

I have been trying to find a good solution for this seemingly simple task for 2 days, and I'm giving up and posting a thread. I hope someone can help me out! I'm on HPUX, using sqlplus, mailx, awk, have some other tools available, but can't install stuff that isn't already in place (without a... (6 Replies)
Discussion started by: soldstatic
6 Replies

7. Shell Programming and Scripting

Parsing a Complicated properties file

Hi All, I have a requirement to parse a file. Let me clear you all on the req. I have a job which contains multiple tasks and each task will have multiple attributes that will be in the below format. Each task will have some sequence number according to that sequence number tasks shld... (1 Reply)
Discussion started by: rajeshorpu
1 Replies

8. Shell Programming and Scripting

CSV file parsing and validation

I have a CSV file that needs to through two seperate processes (in the end there will be 2 files (Dload.unl and Tload.unl and we'll say the input file name is mass.csv). I have a processfile() function that will call the process Dload funtion. In Dload I want to read mass.csv into Dload and then... (1 Reply)
Discussion started by: dolo21taf
1 Replies

9. Shell Programming and Scripting

Parsing a csv file

I am trying to parse a csv file in the below 'name-value pair' format and then use the values corresponding to the name. Type:G,Instance:instance1,FunctionalID:funcid,Env:dev,AppName:appname... (6 Replies)
Discussion started by: chiru_h
6 Replies

10. Shell Programming and Scripting

More complicated log parsing

Hey Guys, I am trying to grep within a file to find and output certain parts of lines to other file(s). The output files need to have a dynamic file name based on a field in the main log. The problem is that every line of the log is not the same, and often not even similar. To explain... (25 Replies)
Discussion started by: sjug
25 Replies
Login or Register to Ask a Question