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.

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')

Hello mohanalakshmi,

Following may help you in same.

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, 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.

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.

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, 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

Just reading file once. Spaces and apostrophes have been made homogeneous, due to inconsistency in OP.

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, 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 
Key=11055005
GiftQuantity=1
MinimumPurchase=400
GiftCatalogIdentifier=('207328014')

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