Fetch the values based on a Key using awk from single file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetch the values based on a Key using awk from single file
# 1  
Old 09-10-2015
RedHat Fetch the values based on a Key using awk from single file

Hi,
Please help to fetch the values for a key from below data format in linux.
HTML Code:
Sample Input Data Format

11055005|PurchaseCondition|GiftQuantity|1
11055005|PurchaseCondition|MinimumPurchase|400
11055005|GiftCatalogEntryIdentifier|Id|207328014
11429510|PurchaseCondition|GiftQuantity|1
11429510|PurchaseCondition|MinimumPurchase|100
11429510|GiftCatalogEntryIdentifier|Id|205955413
11429510|GiftCatalogEntryIdentifier|Id|205955533
11429510|GiftCatalogEntryIdentifier|Id|205955364

Needed output Format

Key = 11055005 
GiftQuantity=1
MinimumPurchase=400
GiftCatalogEntryIdentifier= (207328014)

Key=11429510
GiftQuantity=1
MinimumPurchase=100
GiftCatalogEntryIdentifier= ('205955413','205955533','205955364')
# 2  
Old 09-10-2015
Hello mohanalakshmi,

Following may help you in same.
Code:
awk -F"|" FNR==NR'{A[$1 FS $3]=A[$1 FS $3]?A[$1 FS $3] s1 "," s1 $NF:$NF;next} {D=$1 FS $3} (D in A){X[$1]=X[$1]?X[$1] OFS A[D]:A[D]} END{for(i in X){split(X[i], Q," ");print "Key = " i ORS "GiftQuantity=" Q[1] ORS "MinimumPurchase=" Q[2] ORS "GiftCatalogEntryIdentifier= " s2 s1 Q[3] s1 s3 ORS}}' s1="'" s2="(" s3=")"  Input_file  Input_file

Output will be as follows.
Code:
Key = 11055005
GiftQuantity=1
MinimumPurchase=400
GiftCatalogEntryIdentifier= ('207328014')
 
Key = 11429510
GiftQuantity=1
MinimumPurchase=100
GiftCatalogEntryIdentifier= ('205955413','205955533','205955364')

EDIT: Adding a non one-liner form of solution now.
Code:
awk -F"|" FNR==NR'{
                        A[$1 FS $3]=A[$1 FS $3]?A[$1 FS $3] s1 "," s1 $NF:$NF;
                        next
                  }
                  {
                        D=$1 FS $3
                  }
                        (D in A){
                                        X[$1]=X[$1]?X[$1] OFS A[D]:A[D]
                                }
          END     {
                        for(i in X){
                                        split(X[i], Q," ");
                                        print "Key = " i ORS "GiftQuantity=" Q[1] ORS "MinimumPurchase=" Q[2] ORS "GiftCatalogEntryIdentifier= " s2 s1 Q[3] s1 s3 ORS
                                   }
                  }
                 ' s1="'" s2="(" s3=")" Input_file  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-10-2015 at 12:33 PM.. Reason: Added a non one-liner form of solution now.
# 3  
Old 09-10-2015
Just reading file once. Spaces and apostrophes have been made homogeneous, due to inconsistency in OP.
Code:
awk -F"|" '
{
    k[$1]=k[$1] OFS $4 # build data string containing giftquantity, minimumpurchase and giftids for each key
}
END{                   # after the whole file has been quantified
    for(i in k){       # for each key string do the following
        n=split(k[i], r, OFS)     # divide the data into tokens
        for(e=3;e<=n;e++){        # iterate through giftids found
            ids=(ids) ? ids "," q r[e] q : q r[e] q  # build the formated giftids section
        }
        # output the whole nine yards
        printf "Key=%s\nGiftQuantity=%s\nMinimumPurchase=%s\nGiftCatalogIdentifier=(%s)\n\n", i, r[1], r[2], ids
    }
}' q="'" mohanalakshmi.file

Code:
Key=11055005
GiftQuantity=1
MinimumPurchase=400
GiftCatalogIdentifier=('207328014')

Key=11429510
GiftQuantity=1
MinimumPurchase=100
GiftCatalogIdentifier=('207328014','205955413','205955533','205955364')


Last edited by Aia; 09-10-2015 at 02:15 PM.. Reason: Added comments
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to look up values in File 2 from File 1, & printingNth field of File1 based value of File2 $2

I have two files which are the output of a multiple choice vocab test (60 separate questions) from 104 people (there are some missing responses) and the question list. I have the item list in one file (File1) Item,Stimulus,Choice1,Choice2,Choice3,Choice4,Correct... (5 Replies)
Discussion started by: samonl
5 Replies

2. Shell Programming and Scripting

Taking key values from one file and extracting values from another file

Hi, I have two files with values in both. File1: cat 2 3 dog 4 5 elephant 6 7 camel 2 3 File2: ----+--gkf;ajf= ---+---- +----- cat -------=----+ 3 | 4 ----- dog ------++-- 5 | 9 ----++-- elephant | 5 | 7 ---++ camel ------ ++++_---- || 8 | 9 I want the final file as: cat 4... (1 Reply)
Discussion started by: npatwardhan
1 Replies

3. Shell Programming and Scripting

awk - Merge two files based on one key

Hi, I am struggling with the an awk command to merge two files based on a common key. I want to append the value from File2 ($2) onto the end of File1 where $1 from each file matches - If no match then nothing is apended File1 COL1|COL2|COL3|COL4|COL5|COL6|COL7... (3 Replies)
Discussion started by: Ads89
3 Replies

4. Shell Programming and Scripting

Fetch parent value based on column values

Hi All, I am trying to achieve the below logic, could you please help me in this. In second row 2nd column I've Value JC2 and the same JC2 is 4th row 1st column.So I need to replace JC2 value in 4th row with JC2 2nd row's 1st column. Input: Job1,JC1 Job1,JC2 Job1,JC3 JC2,JA1... (6 Replies)
Discussion started by: unme
6 Replies

5. Shell Programming and Scripting

How to fetch values from a line in a file to variables in UNIX?

Hi, I need to assign values from a lines in a file into variables in unix, i am using Korn shell. I tried the below script from posts but i am unable to fetch every value in a variable. #! /usr/bin/ksh #for file in test.txt; do IFS=$'\|' I=1 while read -a val do echo... (15 Replies)
Discussion started by: karthikram
15 Replies

6. Shell Programming and Scripting

Show distinct values of a key from a single line

Hi All, I am newbie to linux. Can somebody help me with following requirement. I have one huge line. I have to find out particular key/value pair to see the distinct value of that key. Portion of the String:- <?xml version="1.1" encoding="UTF-8"?> <Data><Val Ti="1342750845538" Du="0"... (5 Replies)
Discussion started by: kmajumder
5 Replies

7. Shell Programming and Scripting

awk - splitting 1 large file into multiple based on same key records

Hello gurus, I am new to "awk" and trying to break a large file having 4 million records into several output files each having half million but at the same time I want to keep the similar key records in the same output file, not to exist accross the files. e.g. my data is like: Row_Num,... (6 Replies)
Discussion started by: kam66
6 Replies

8. Shell Programming and Scripting

Gawk / Awk Merge Lines based on Key

Hi Guys, After windows died on my netbook I installed Lubuntu and discovered Gawk about a month ago. After using Excel for 10+ years I'm amazed how quick and easily Gawk can process data but I'm stuck with a little problem merging data from multiple lines. I'm an SEO Consultant and provide... (9 Replies)
Discussion started by: Jamesfirst
9 Replies

9. Shell Programming and Scripting

Print a key with its all values using awk/others

input COL1 a1 b1 c1 d1 e1 f1 C1 10 10 10 100 100 1000 C2 20 20 200 200 200 2000 output C1 a1 10 1 C1 b1 10 1 C1 c1 10 1 C1 d1 100 2 C1 e1 100 2 C1 f1 1000 3 C2 ... (12 Replies)
Discussion started by: ruby_sgp
12 Replies

10. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies
Login or Register to Ask a Question