Print a key with its all values using awk/others


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print a key with its all values using awk/others
# 8  
Old 05-24-2010
Hi ruby_sgp,

Do you mean something like this:
Code:
awk '{ if (NR==1)
         for (i=1;i<=NF;i++) L[i]=$i
       else {
         v=1
         delete O
         for (i=2;i<=NF;i++) {
           if (!O[$i])
             O[$i]=v++
           if ( O[$i] > 2 )
             O[$i]="?"
           print $1"\t"L[i]"\t"$i"\t"O[$i]
         }
       }
     }' infile

Output:
Code:
I_1     a1      a/a     1
I_1     a2      a/a     1
I_1     a3      a/b     2
I_1     a6      a/b     2
I_1     a8      a/a     1
I_1     a9      b/b     ?
I_2     a1      b/b     1
I_2     a2      b/b     1
I_2     a3      b/b     1
I_2     a6      b/b     1
I_2     a8      b/c     2
I_2     a9      b/d     ?
I_3     a1      c/c     1
I_3     a2      c/c     1
I_3     a3      c/d     2
I_3     a6      c/e     ?
I_3     a8      c/e     ?
I_3     a9      c/e     ?


Last edited by Scrutinizer; 05-24-2010 at 04:37 AM..
# 9  
Old 05-24-2010
hi

But I'm getting this output instead of your posted output?

Code:
I_1     a1      a/a     1
I_1     a2      a/a     1
I_1     a3      a/b     ?
I_1     a6      a/b     ?
I_1     a8      a/a     ?
I_1     a9      b/b     ?
I_2     a1      b/b     1
I_2     a2      b/b     1
I_2     a3      b/b     1
I_2     a6      b/b     1
I_2     a8      b/c     ?
I_2     a9      b/d     ?
I_3     a1      c/c     1
I_3     a2      c/c     1
I_3     a3      c/d     ?
I_3     a6      c/e     ?
I_3     a8      c/e     ?
I_3     a9      c/e     ?


Last edited by ruby_sgp; 05-24-2010 at 04:34 AM.. Reason: Didn't check the script
# 10  
Old 05-24-2010
Oops, that was because of a small last minute change. I changed it back in my post. See if it works now.
This should also work:
Code:
awk '{ if (NR==1)
         for (i=1;i<=NF;i++) L[i]=$i
       else {
         v=0
         delete O
         for (i=2;i<=NF;i++) {
           if (!O[$i])
             O[$i]=(++v<3)?v:"?"
           print $1"\t"L[i]"\t"$i"\t"O[$i]
         }
       }
     }' infile

# 11  
Old 05-24-2010
hey

working great. thanx for your time. really appreciated
# 12  
Old 05-28-2010
I found a small error

FYI:
1st rule similar letter of key should be always 1 or 3 (a/a is 1 or 3 not 2)
2nd rule dissimilar letters of a key should be always 2 not 3
3rd rule second occurrence of dissimilar letters of key should be always ?

Code:
I_1    a1    a/a    1 (ok)
I_1    a2    a/a    1 (ok)
I_1    a3    a/b    2 (ok)
I_1    a6    a/b    2 (ok)
I_1    a8    a/a    1 (ok)
I_1    a9    b/b    ? (it should be 3)
I_2    a1    b/b    1 (ok)
I_2    a2    b/b    1 (ok)
I_2    a3    b/b    1 (ok)
I_2    a6    b/b    1 (ok)
I_2    a8    b/c    2 (ok)
I_2    a9    b/d    ? (ok)
I_3    a1    c/c    1 (ok)
I_3    a2    c/c    1 (ok)
I_3    a3    c/d    2 (ok)
I_3    a6    c/e    ? (ok)
I_3    a8    c/e    ? (ok)
I_3    a9    c/e    ? (ok)

# 13  
Old 06-02-2010
For example

Your script produce some thing like this

input

Code:
IndID    ID1    ID2    ID3    ID4    ID5    ID6
a1    N/N    a/a    a/b    a/b    a/a    b/b
a2    b/b    b/b    b/b    b/b    b/c    b/d
a3    c/d    c/d    c/d    c/d    c/d    c/d

output
Code:
a1      ID1     N/N     1
a1      ID2     a/a     2
a1      ID3     a/b     ?
a1      ID4     a/b     ?
a1      ID5     a/a     2
a1      ID6     b/b     ?
a2      ID1     b/b     1
a2      ID2     b/b     1
a2      ID3     b/b     1
a2      ID4     b/b     1
a2      ID5     b/c     2
a2      ID6     b/d     ?
a3      ID1     c/d     1
a3      ID2     c/d     1
a3      ID3     c/d     1
a3      ID4     c/d     1
a3      ID5     c/d     1
a3      ID6     c/d     1

Should be like this

Code:
a1      ID1     N/N     ?
a1      ID2     a/a     1
a1      ID3     a/b     2
a1      ID4     a/b     2
a1      ID5     a/a     1
a1      ID6     b/b     3
a2      ID1     b/b     1
a2      ID2     b/b     1
a2      ID3     b/b     1
a2      ID4     b/b     1
a2      ID5     b/c     2
a2      ID6     b/d     ?
a3      ID1     c/d     1
a3      ID2     c/d     2
a3      ID3     c/d     2
a3      ID4     c/d     2
a3      ID5     c/d     2
a3      ID6     c/d     2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print values within groups of lines with awk

Hello to all, I'm trying to print the value corresponding to the words A, B, C, D, E. These words could appear sometimes and sometimes not inside each group of lines. Each group of lines begins with "ZYX". My issue with current code is that should print values for 3 groups and only is... (6 Replies)
Discussion started by: Ophiuchus
6 Replies

2. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: mohanalakshmi
2 Replies

3. UNIX for Dummies Questions & Answers

awk - Print lines if only matching key is found

I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. Thanks a lot. Any help is appreciated. Script I am using: awk 'FNR == NR && ! /^]*$/ {... (9 Replies)
Discussion started by: High-T
9 Replies

4. Shell Programming and Scripting

awk - Print whole string ending with a Tab if key matched

Hi , I am looking to print the whole string from file2.txt but it is only printing 77 but not the whole matched string from File2.txt Any help is appreciated. Thanks, Script awk ' BEGIN { OFS="\t" out = "a.txt"} NR==FNR && NF {a=$0; next} function print_65_11() { if... (11 Replies)
Discussion started by: High-T
11 Replies

5. Shell Programming and Scripting

awk print odd values

value=$(some command) for all in `echo $value` do awk checks each value (all) to see if it is a odd number. if so, prints the value done sounds easy enough but i've been unable to find anything on google. (2 Replies)
Discussion started by: SkySmart
2 Replies

6. Shell Programming and Scripting

How to print in awk matching $1 values ,to $1,$4 example given.?

Hi Experts, I am trying to get the output from a matching pattern but unable to construct the awk command: file : aa bb cc 11 dd aa cc 33 cc 22 45 68 aa 33 44 44 dd aa cc 37 aa 33 44 67 I want the output to be : ( if $1 match to "aa" start of the line,then print $4 of that line, and... (3 Replies)
Discussion started by: rveri
3 Replies

7. Shell Programming and Scripting

match two key columns in two files and print output (awk)

I have two files... file1 and file2. Where columns 1 and 2 of file1 match columns 1 and 2 of file2 I want to create a new file that is all file1 + columns 3 and 4 of file2 :b: Many thanks if you know how to do this.... :b: file1 31-101 106 0 92 31-101 106 29 ... (2 Replies)
Discussion started by: pelhabuan
2 Replies

8. Shell Programming and Scripting

Extract key words and print their values

Input file (HTTP request log file): GET... (2 Replies)
Discussion started by: buptwy
2 Replies

9. UNIX for Advanced & Expert Users

Awk to print values of second file

Hello, I have a data file with 300,000 records in it, and another file which contains only the line numbers of roughly 13,000 records in the data file which have data integrity issues. I'm trying to find a way to print the original data by line number identified in the second file. How can I do... (2 Replies)
Discussion started by: peteroc
2 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