Extracting field values from .csv | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Extracting field values from .csv

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-22-2013
leghorn leghorn is offline
Registered User
 
Join Date: May 2013
Last Activity: 27 August 2013, 4:15 AM EDT
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Extracting field values from .csv

How can I select the bold fields from the following?

Code:
"CLLI","SWREL","RPTDATE","RPTIME","TZ","RPTTYPE","RPTPD","IVALDATE","IVALSTART","IVALEND","NUMENTIDS"
"tklc9010801","EAGLE5 45.0.0-64.70.1","2013-08-07","02:01:50","MST ","COMPONENT MEASUREMENTS ON LINK","LAST","2013-08-07","01:30:00","02:00:00",2800

"STATUS","LSN","LOC","LINK","LNKTYPE","MSGSTRAN","MSGSRCVD","MSURETRN","OCTRETRN","MOCTTRAN","MOCTRCVD","MTCEUSG","DURLKOTG","MSGSRGTT","MOCTRGTT","TDCNGLV1","TDCNGLV2","TDCNGLV3","ECCNGLV1","ECCNGLV2","ECCNGLV3","MSGDISC0","MSGDISC1","MSGDISC2","MSGDISC3","LNKAVAIL","NMGWSDSABL","OUTCELLS","INCCELLS","SDPDUTRN","SDPDURCV","SDPDURTR","LMSUTRN","LMSURCV","LMSUOCTTRN","LMSUOCTRCV","LMSUTRNDSC","LMSURCVDSC","M2PUDMTR","M2PUDOCT","M2PUDMRC","M2PUDOCR","M2PLKNIS","ECLNKCB","ECLNKXCO"
"K","ls1101i00","1201","A  ","IPVL",0,0,0,0,0,0,0,1800,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
"K","ls1101n04","1201","B  ","IPVL",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1800,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

please generalize it as the values can be different as well

~thanks

Last edited by vbe; 08-22-2013 at 04:41 AM..
Sponsored Links
    #2  
Old 08-22-2013
rajamadhavan rajamadhavan is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 24 April 2014, 4:32 AM EDT
Location: Bangalore, India
Posts: 279
Thanks: 4
Thanked 58 Times in 57 Posts
@leghorn, You have not made it "2013-08-07","02:01:50" bold. What is the logic on which you need to extract the date and time fields?
Sponsored Links
    #3  
Old 08-22-2013
leghorn leghorn is offline
Registered User
 
Join Date: May 2013
Last Activity: 27 August 2013, 4:15 AM EDT
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Hi
yeah I did not,becoz they are not needed
please see that there are 11 fields in the first line .. "CLLI","SWREL" ...
corresponding to these there are values in the next line ..
so the value of IVALDATE is 2013-08-07 and so on ..
these fields are always on the 8th,9th and 10th tabs
I need the values of "IVALDATE","IVALSTART" and "IVALEND"
    #4  
Old 08-22-2013
rajamadhavan rajamadhavan is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 24 April 2014, 4:32 AM EDT
Location: Bangalore, India
Posts: 279
Thanks: 4
Thanked 58 Times in 57 Posts
Two options, if the index is same (8th 9th and 10th) and the required values are in the 2nd line


Code:
awk -F, 'NR==2{gsub(/\"/,x);print $8,$9,$10}' file.csv

More generic one would be similar to the one posted on the other thread Perl to shell script - The UNIX and Linux Forums


Code:
awk -F, 'NR==1{gsub(/\"/,x);for(i=1;i<=NF;i++){a[i]=$i;};getline;gsub(/\"/,x);for(i=1;i<=NF;i++){array[a[i]]=$i}} END{print array["IVALDATE"],array["IVALSTART"],array["IVALEND"]}' file.csv

Sponsored Links
    #5  
Old 08-22-2013
leghorn leghorn is offline
Registered User
 
Join Date: May 2013
Last Activity: 27 August 2013, 4:15 AM EDT
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
1st one looks good .. how do I save these values in the separate variables

thnx anyway,big help
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
CSV with commas in field values, remove duplicates, cut columns krishnix Shell Programming and Scripting 4 12-08-2011 01:25 AM
adding field values if field matches raghavendra.cse Shell Programming and Scripting 4 07-07-2010 09:37 AM
Find top N values for field X based on field Y's value FrancoisCN Shell Programming and Scripting 1 05-29-2009 09:57 AM
Extracting Field values for XML file junaid.nehvi Programming 3 03-27-2009 12:38 PM
Need to compare two csv files values and write into another csv file chinnahyd Shell Programming and Scripting 2 02-06-2009 01:17 PM



All times are GMT -4. The time now is 07:52 PM.