Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Pick the column value based on another column from .csv file

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-18-2013
Ganesh L Ganesh L is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 22 January 2015, 5:48 PM EST
Posts: 21
Thanks: 10
Thanked 1 Time in 1 Post
Pick the column value based on another column from .csv file

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated.
Main imp point, in my .csv file, third column is having price value with comma (1,20,300), it has to be considered 1,000 as single column.
Please someone help me to resolve this issue.

Source file format

Code:
column1 column2 column 3 column4 column5
1,ganesh,1,000,2,1
222,ram,2,000,2|3,1
222,ram,5,000,2|3,2
33,raju,5,000,2|5|4|6|1,1
33,raju,5,000,2|8|4|6|1,2
33,raju,5,000,2|9|4|6|1,3
33,raju,5,000,2|8|4|6|1,4
33,raju,5,000,2|3|4|6|1,5

Target output


Code:
column1 column2 column3 column4 
1,ganesh,1,000,2,1
222,ram,2,000,2,1
222,ram,5,000,3,2
33,raju,5,000,2,1
33,raju,5,000,8,2
33,raju,5,000,4,3
33,raju,5,000,6,4
33,raju,5,000,1,5


Last edited by Scott; 01-18-2013 at 04:54 AM.. Reason: Please use code tags
Sponsored Links
    #2  
Old Unix and Linux 01-18-2013
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 13 January 2015, 10:34 AM EST
Location: Houston, Texas, USA
Posts: 681
Thanks: 0
Thanked 206 Times in 198 Posts
try:

Code:
awk -F, '/,/ {split($5,arr, "|"); $5=arr[$6]} 1' OFS=, infile

Sponsored Links
    #3  
Old Unix and Linux 01-18-2013
Ganesh L Ganesh L is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 22 January 2015, 5:48 PM EST
Posts: 21
Thanks: 10
Thanked 1 Time in 1 Post
Thanks for the solution... I will try this code and let u know the status
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pick the column value based on another column using awk or CUT Ganesh L Shell Programming and Scripting 2 01-09-2013 02:06 AM
Read CSV column value based on column name JohnGG Shell Programming and Scripting 3 11-20-2009 12:33 AM
extract csv based on column value maruthavanan Shell Programming and Scripting 3 11-12-2009 09:55 AM
How to pick values from column based on key values by usin AWK repinementer Shell Programming and Scripting 16 07-24-2009 08:59 AM
sorting csv file based on column selected tententen Shell Programming and Scripting 4 07-05-2009 10:17 PM



All times are GMT -4. The time now is 03:49 AM.