Parsing a csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing a csv file
# 1  
Old 02-11-2008
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
Type:A,AppName:appname,ProcessNameSmilierocess1.ear,Machine:machine1|Mode:FT|Setting:P,Machine:machine2| Mode:FT|Setting:B
Type:A,AppName:appname,ProcessNameSmilierocess2.ear,Machine:machine1|Mode:FT|Setting:P,Machine:machine2| Mode:FT|Setting:B

I have to get the the value instance1(from Instance) dynamically by looking for Type:G and similarly other variables corresponding to their name pair. And then from Type:A, I have to compare AppName with Type:G and get the variables corresponding to the name pairs. Please note that if there are more than two values, there is an other delimitter '|' - Machine:machine1|Mode:FT|Setting:P

I am trying to use sed , cut etc., but couldn't hit the right logic. I really appreciate if I get a solution here.

Thanks,
Chiru
# 2  
Old 02-11-2008
a little more info

Can you provide the expected/desired output?
Also, to help with exception & rule-handling, perhaps more input lines.
# 3  
Old 02-11-2008
The output is that I should get each value corresponding to the name-value pair from the csv file. I am using these variables in another scripts. For example, from the line Type:G,Instance:instance1,FunctionalID:funcid,Env:dev,AppName:appname
I should get the value instance1 corresponding to Instance. I am trying to look for Instance and get the value next to Instance:
In the script I am exporting the value of instance1 as export INSTANCE=instance1

I hope it is clear and please let me know how can I do that.

Thanks,
Chiru
# 4  
Old 02-11-2008
Simply, here is what I am looking for:
from the csv file that has below line:
Type:G,Instance:instance1,FunctionalID:funcid,Env:dev,AppName:appname

The output may be as a file or be a variable:

INSTANCE=instance1
FUNCID=funcid
ENV=dev
APPNAME=appname

Thanks,
Ravi
# 5  
Old 02-11-2008
awk

Hi,
Actually i am not sure about your requirements, could you please specific on it?

According to your last thread, i think below code can address your issue.

Code:
echo "Type:G,Instance:instance1,FunctionalID:funcid,Env:dev,AppName:appname" | sed 's/:/=/g' | awk 'BEGIN{FS=","}
{
print $2
print $3
print $4
print $5
}'

# 6  
Old 02-11-2008
Well, maybe I've put my problem in a complicated way - excuse my English Smilie

There is an input file(csv file)which has the following lines:

cat csvfile.csv
Type:G,Instance:instance1,FunctionalID:funcid,Env:dev,AppName:appname
Type:A,AppName:appname,ProcessNamerocess1.ear,Machine:machine1|Setting:P,Machine:machine2|Setting:B
Type:A,AppName:appname,ProcessNamerocess2.ear,Machine:machine1|Setting:P,Machine:machine2|Setting:B

This file is created as a name-value pair like Instance:instance1.

From this file, I need to extract 'instance1' variable, and similarly all other variables. I am using these variables and using in another script to export those variables.
# 7  
Old 02-12-2008
The one challenge I am seeing is that these name-value pairs in the csv file can be in different order. For example,
cat csvfile.csv
Type:G,Instance:instance1,FunctionalID:funcid,Env:dev,AppName:appname
....

cat csvfile.csv
FunctionalID:funcid,Env:dev,Type:G,Instance:instance1,AppName:appname

So I cannot just say $2 or $3 to get instance1. I have to look for corresponding name 'Instance' and then from that, derive its value-instance1.

Any ideas??
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 and deleting all rows on condition

Hello list, I am working on a csv file which contains two fields per record which contain IP addresses. What I am trying to do is find records which have identical fields(IP addresses) which occur 4(four) times, and if they do, delete all records with that specific identical field(ip address). ... (4 Replies)
Discussion started by: landossa
4 Replies

6. Shell Programming and Scripting

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: "Apple","Apples, are fun",3.60,4.4,"I... (3 Replies)
Discussion started by: analog999
3 Replies

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

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

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

10. Shell Programming and Scripting

Help in parsing a CSV file with Shell script

I have a CSV file which contains number series as one of the fields. Some of the records of the field look like : 079661/3 I have to convert the above series as 079661 079662 079663 and store it as 3 different records. Looking for help on how to achieve this. Am a newbie at Shell... (10 Replies)
Discussion started by: mihirk
10 Replies
Login or Register to Ask a Question