The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help me with parsing this file eamani_sun Shell Programming and Scripting 2 05-16-2008 12:39 PM
awk and file parsing devtakh Shell Programming and Scripting 4 05-06-2008 08:13 AM
Parsing xml file using Sed kapilkinha UNIX for Advanced & Expert Users 3 04-08-2008 06:43 AM
File Parsing jsusheel Shell Programming and Scripting 5 09-25-2007 07:25 AM
parsing file through awk bbeugie Shell Programming and Scripting 13 08-22-2006 10:21 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-11-2008
Registered User
 

Join Date: Jul 2006
Posts: 68
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,ProcessNamerocess1.ear,Machine:machine1|Mode:FT|Setting:P,Machine:machine2| Mode:FT|Setting:B
Type:A,AppName:appname,ProcessNamerocess2.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
Reply With Quote
Forum Sponsor
  #2  
Old 02-11-2008
joeyg's Avatar
Moderator
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 983
a little more info

Can you provide the expected/desired output?
Also, to help with exception & rule-handling, perhaps more input lines.
Reply With Quote
  #3  
Old 02-11-2008
Registered User
 

Join Date: Jul 2006
Posts: 68
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
Reply With Quote
  #4  
Old 02-11-2008
Registered User
 

Join Date: Jul 2006
Posts: 68
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
Reply With Quote
  #5  
Old 02-11-2008
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 495
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
}'
Reply With Quote
  #6  
Old 02-11-2008
Registered User
 

Join Date: Jul 2006
Posts: 68
Well, maybe I've put my problem in a complicated way - excuse my English

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.
Reply With Quote
  #7  
Old 02-12-2008
Registered User
 

Join Date: Jul 2006
Posts: 68
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??
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0