![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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,ProcessName Type:A,AppName:appname,ProcessName 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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
a little more info
Can you provide the expected/desired output?
Also, to help with exception & rule-handling, perhaps more input lines. |
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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?? |
|||
| Google The UNIX and Linux Forums |