awk Search Array Element Return Index


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk Search Array Element Return Index
# 8  
Old 12-10-2013
Struggling to obtain the correct output format.

What I want is (problem name and hash values should line up):
Code:
Source_Name Problem_Name Hash
102030         102080           BEE77DA5BE9F24FBE044002128B2E77C
                   102040           BEE77DA5BE9F24FBE044002128B2E77C

Amended script:
Code:
nawk -F= '
        BEGIN {
                SRC["102030"] = "BEE77DA5BE9F24FBE044002128B2E77C"
                CMP["102030"] = "BEE77DA5BE9F24FBE044002128B2E77C"
                CMP["102040"] = "BEE77DA5BE9F24FBE044002128B2E77C"
                CMP["102050"] = "AEE77123402128B2EF24FBEE77D28B2E"
                CMP["102080"] = "BEE77DA5BE9F24FBE044002128B2E77C"
        }
        END {
                printf "Source_Name" " Problem_Name Hash\n"
                for ( v in SRC )
                {
                        for ( k in CMP )
                        {
                               if ( k == v )
                                        printf k
                               else if ( CMP[k] == SRC[v] )
                                        printf "%+13s\n",k CMP[k]
                        }
                }
        }
' /dev/null

Output:
Code:
Source_Name Problem_Name Hash
102030102080BEE77DA5BE9F24FBE044002128B2E77C
102040BEE77DA5BE9F24FBE044002128B2E77C


Last edited by u20sr; 12-10-2013 at 08:23 AM..
# 9  
Old 12-10-2013
Quote:
Originally Posted by u20sr
Struggling to obtain the correct output format.

What I want is (problem name and hash values should line up):
Code:
Source_Name Problem_Name Hash
102030         102080           BEE77DA5BE9F24FBE044002128B2E77C
                   102040           BEE77DA5BE9F24FBE044002128B2E77C

Amended script:
Code:
nawk -F= '
        BEGIN {
                SRC["102030"] = "BEE77DA5BE9F24FBE044002128B2E77C"
                CMP["102030"] = "BEE77DA5BE9F24FBE044002128B2E77C"
                CMP["102040"] = "BEE77DA5BE9F24FBE044002128B2E77C"
                CMP["102050"] = "AEE77123402128B2EF24FBEE77D28B2E"
                CMP["102080"] = "BEE77DA5BE9F24FBE044002128B2E77C"
        }
        END {
                printf "Source_Name" " Problem_Name Hash\n"
                for ( v in SRC )
                {
                        for ( k in CMP )
                        {
                               if ( k == v )
                                        printf k
                               else if ( CMP[k] == SRC[v] )
                                        printf "%+13s\n",k CMP[k]
                        }
                }
        }
' /dev/null

Output:
Code:
Source_Name Problem_Name Hash
102030102080BEE77DA5BE9F24FBE044002128B2E77C
102040BEE77DA5BE9F24FBE044002128B2E77C

modify
Code:
else if ( CMP[k] == SRC[v] )
                                        printf "%+13s\t%s\n",k,CMP[k]

Separators were missing
This User Gave Thanks to Akshay Hegde For This Post:
# 10  
Old 12-10-2013
Almost, 102040 should line up with 102080.

Code:
Source_Name Problem_Name Hash
102030      102080      BEE77DA5BE9F24FBE044002128B2E77C
      102040    BEE77DA5BE9F24FBE044002128B2E77C

Maybe I'm going about it the wrong way. How would I set a fixed starting point for:

Code:
printf "%+13s\t%s\n",k,CMP[k]

# 11  
Old 12-10-2013
Try
Code:
        for ( k in CMP )
                {printf "%13s\t", (k==v)?k:""
                 if ( CMP[k] == SRC[v] )
                        printf "%+13s\t%s\n",k,CMP[k]
                }

This User Gave Thanks to RudiC For This Post:
# 12  
Old 12-10-2013
Not quite but I can see where you're going. I'll play about with it a bit.

Code:
Source_Name Problem_Name Hash
      102030          102030    BEE77DA5BE9F24FBE044002128B2E77C
                      102080    BEE77DA5BE9F24FBE044002128B2E77C
                                      102040    BEE77DA5BE9F24FBE044002128B2E77C

---------- Post updated at 02:38 PM ---------- Previous update was at 01:59 PM ----------

Sod it. Made it easier.

Code:
for ( k in CMP )
                        {
                              if ( CMP[k] == SRC[v] )
                                  printf ("%-12s%-13s%s\n",v,k,CMP[k]) 
                        }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: check element in array and it's value

Hello, I want to see if element exists in array, if so then, check it's corresponding value. Column 4 is position and column 1 is the chromosome for it. There are duplicates for one position on one chromosome. I want to check if same position exists on different chromosome: Data... (8 Replies)
Discussion started by: genome
8 Replies

2. UNIX for Beginners Questions & Answers

Awk: count unique element of array

Hi, tab-separated input: blabla_1 A,B,C,C blabla_2 A,E,G blabla_3 R,Q,A,B,C,R,Q output: blabla_1 3 blabla_2 3 blabla_3 5 After splitting $2 in an array, I am trying to store the number of unique elements in a variable, but have some difficulties resetting the variable to 0 before... (6 Replies)
Discussion started by: beca123456
6 Replies

3. Shell Programming and Scripting

Index problem in associate array in awk

I am trying to reformat the table by filling any missing rows. The final table will have consecutive IDs in the first column. My problem is the index of the associate array in the awk script. infile: S01 36407 53706 88540 S02 69343 87098 87316 S03 50133 59721 107923... (4 Replies)
Discussion started by: yifangt
4 Replies

4. Shell Programming and Scripting

Search an array and return index (bash)

Hi all, In bash, is there any way of searching an array and returning the index? For example, how could I write a script that would do the following: >> search note_array=(C D E F G A B) for F return the value 3 (or 4) Thanks, R (5 Replies)
Discussion started by: RMontenegro
5 Replies

5. Shell Programming and Scripting

how to search array and print index in ksh

Hi, I am using KSH shell to do some programming. I want to search array and print index value of the array. Example.. nodeval4workflow="DESCRIPTION ="" ISENABLED ="YES" ISVALID ="YES" NAME="TESTVALIDATION" set -A strwfVar $nodeval4workflow strwfVar=DESCRIPTION=""... (1 Reply)
Discussion started by: tmalik79
1 Replies

6. Shell Programming and Scripting

How to check index of a array element in shell script?

Example - Script to find the index of a month from array MONTHS="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec" set -A MON $MONTHS A="Sun May 23 09:34:30 GMT 2010" getMonth=`echo $A|cut -c5-7` ##getMonth=May Arrayindex_in_MONTHS_array= ???? # { 0,1,2,3,4 } - at fifth place ... (7 Replies)
Discussion started by: KuldeepSinghTCS
7 Replies

7. Shell Programming and Scripting

awk: reading into an array and then print the value corresponding to index

I am beginner in awk awk 'BEGIN{for(i=1;(getline<"opnoise")>0;i++) arr=$1}{print arr}' In the above script, opnoise is a file, I am reading it into an array and then printing the value corresponding to index 20. Well this is not my real objective, but I have posted this example to describe... (19 Replies)
Discussion started by: akshaykr2
19 Replies

8. Shell Programming and Scripting

awk array index help

$ cat file.txt A|X|20 A|Y|20 A|X|30 A|Z|20 B|X|10 A|Y|40 Summing up $NF based on first 2 fields, $ awk -F "|" 'BEGIN {OFS="|"} { sum += $NF } END { for (f in sum) print f,sum } ' file.txt o/p: A|X|50 A|Y|60 A|Z|20 (4 Replies)
Discussion started by: uwork72
4 Replies

9. UNIX for Dummies Questions & Answers

wh inode index starts from 1 unlike array index (0)

brothers why inode index starts from 1 unlike array inex which starts from 0 its a question from the design of unix operating system of maurice j.bach i need to know the answer urgently...someone help please (1 Reply)
Discussion started by: sairamdevotee
1 Replies

10. Filesystems, Disks and Memory

why the inode index of file system starts from 1 unlike array index(0)

why do inode indices starts from 1 unlike array indexes which starts from 0 its a question from "the design of unix operating system" of maurice j bach id be glad if i get to know the answer quickly :) (0 Replies)
Discussion started by: sairamdevotee
0 Replies
Login or Register to Ask a Question