The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 03:39 PM
awk and file parsing devtakh Shell Programming and Scripting 4 05-06-2008 11:13 AM
Parsing xml file using Sed kapilkinha UNIX for Advanced & Expert Users 3 04-08-2008 09:43 AM
File Parsing jsusheel Shell Programming and Scripting 5 09-25-2007 10:25 AM
parsing file through awk bbeugie Shell Programming and Scripting 13 08-22-2006 01:21 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-11-2008
chiru_h chiru_h is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 72
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
  #2 (permalink)  
Old 02-11-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
a little more info

Can you provide the expected/desired output?
Also, to help with exception & rule-handling, perhaps more input lines.
  #3 (permalink)  
Old 02-11-2008
chiru_h chiru_h is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 72
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 (permalink)  
Old 02-11-2008
chiru_h chiru_h is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 72
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 (permalink)  
Old 02-11-2008
summer_cherry summer_cherry is online now Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,079
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 (permalink)  
Old 02-11-2008
chiru_h chiru_h is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 72
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.
  #7 (permalink)  
Old 02-12-2008
chiru_h chiru_h is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 72
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??
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:50 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0