Sponsored Content
Top Forums Shell Programming and Scripting Fetch the values based on a Key using awk from single file Post 302954727 by Aia on Thursday 10th of September 2015 12:01:48 PM
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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
HTML::Microformats::Format::hRecipe(3pm)		User Contributed Perl Documentation		  HTML::Microformats::Format::hRecipe(3pm)

NAME
HTML::Microformats::Format::hRecipe - the hRecipe microformat SYNOPSIS
use Data::Dumper; use HTML::Microformats::DocumentContext; use HTML::Microformats::Format::hRecipe; my $context = HTML::Microformats::DocumentContext->new($dom, $uri); my @recipes = HTML::Microformats::Format::hRecipe->extract_all( $dom->documentElement, $context); foreach my $recipe (@recipes) { print $recipe->get_summary . " "; } DESCRIPTION
HTML::Microformats::Format::hRecipe inherits from HTML::Microformats::Format. See the base class definition for a description of property getter/setter methods, constructors, etc. MICROFORMAT
HTML::Microformats::Format::hRecipe supports hRecipe 0.23 as described at <http://microformats.org/wiki/hrecipe>. RDF OUTPUT
<http://linkedrecipes.org/schema/>, <http://ontologi.es/hrecipe#>. BUGS
Please report any bugs to <http://rt.cpan.org/>. SEE ALSO
HTML::Microformats::Format, HTML::Microformats. AUTHOR
Toby Inkster <tobyink@cpan.org>. COPYRIGHT
Copyright 2008-2011 Toby Inkster This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. perl v5.14.2 2011-12-06 HTML::Microformats::Format::hRecipe(3pm)
All times are GMT -4. The time now is 01:23 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy