2D Array using AWK - pls help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 2D Array using AWK - pls help
# 1  
Old 09-21-2006
2D Array using AWK - pls help

Hi, i want to write a script using AWK to compare the difference between 2 text file using 2 dimensional array. The script should compare each and every symbol display in the 5 by 5 matrix.
The output should display the coordinates (x,y) of where the difference lies, and displays what is the symbol at Text 1 and symbol at Text 2. Pls help. Thanks

Text 1:

05 *
04 * * *
03 ** * * *
02 * * *
01 *
0 0 0 0 0
1 2 3 4 5

Text 2:

05 &
04 * * *
03 ** * * *
02 * * *
01 *
0 0 0 0 0
1 2 3 4 5

Here, we should be expecting the below output:

Coordinates = (03,05)
Symbol at Text 1 = *
Symbol at Text 2 = &
# 2  
Old 09-21-2006
Try and adapt the following awk program cmp.awk :
Code:
BEGIN {
   yy[1] = yy[2] = 5;
   for (x=1; x<=5; x++)
      for (y=1; y<=5; y++)
         array[1, x, y] = array[2, x, y] = " ";
}
FNR > 5 {
   next;
}
{
   arr = (NR == FNR ? 1 : 2);
   y   = yy[arr];
   $0  = $0 "     ";
   for (x=1; x<=5; x++) 
      array[arr, x, y] = substr($0, x, 1);
   yy[arr] -= 1;
}
END {
   for (x=1; x<=5; x++)
      for (y=1; y<=5; y++)
         if (array[1, x, y] != array[2, x, y]) {
            printf("Coordinates = (%02d,%02d)\n", x, y);
            printf("Symbol at Text 1 =  %s\n", array[1, x, y]);
            printf("Symbol at Text 2 =  %s\n", array[2, x, y]);
         }
}

The execution of the program gives :
Code:
$ cat text1
*
* * *
* * * * *
* * *
*
$ cat text2
&
* * *
* * * * *
* * *
*
$ awk -f cmp.awk text1 text2
Symbol at Text 1 =  *
Symbol at Text 2 =  &
$

Jean-Pierre.
# 3  
Old 09-21-2006
Quote:
Originally Posted by aigles
Try and adapt the following awk program cmp.awk :
Code:
BEGIN {
   yy[1] = yy[2] = 5;
   for (x=1; x<=5; x++)
      for (y=1; y<=5; y++)
         array[1, x, y] = array[2, x, y] = " ";
}
FNR > 5 {
   next;
}
{
   arr = (NR == FNR ? 1 : 2);
   y   = yy[arr];
   $0  = $0 "     ";
   for (x=1; x<=5; x++) 
      array[arr, x, y] = substr($0, x, 1);
   yy[arr] -= 1;
}
END {
   for (x=1; x<=5; x++)
      for (y=1; y<=5; y++)
         if (array[1, x, y] != array[2, x, y]) {
            printf("Coordinates = (%02d,%02d)\n", x, y);
            printf("Symbol at Text 1 =  %s\n", array[1, x, y]);
            printf("Symbol at Text 2 =  %s\n", array[2, x, y]);
         }
}

The execution of the program gives :
Code:
$ cat text1
*
* * *
* * * * *
* * *
*
$ cat text2
&
* * *
* * * * *
* * *
*
$ awk -f cmp.awk text1 text2
Symbol at Text 1 =  *
Symbol at Text 2 =  &
$

Jean-Pierre.
which value is belong to file - test1 and test2?
# 4  
Old 09-21-2006
Quote:
Originally Posted by aigles
Try and adapt the following awk program cmp.awk :
Code:
BEGIN {
   yy[1] = yy[2] = 5;
   for (x=1; x<=5; x++)
      for (y=1; y<=5; y++)
         array[1, x, y] = array[2, x, y] = " ";
}
FNR > 5 {
   next;
}
{
   arr = (NR == FNR ? 1 : 2);
   y   = yy[arr];
   $0  = $0 "     ";
   for (x=1; x<=5; x++) 
      array[arr, x, y] = substr($0, x, 1);
   yy[arr] -= 1;
}
END {
   for (x=1; x<=5; x++)
      for (y=1; y<=5; y++)
         if (array[1, x, y] != array[2, x, y]) {
            printf("Coordinates = (%02d,%02d)\n", x, y);
            printf("Symbol at Text 1 =  %s\n", array[1, x, y]);
            printf("Symbol at Text 2 =  %s\n", array[2, x, y]);
         }
}

The execution of the program gives :
Code:
$ cat text1
*
* * *
* * * * *
* * *
*
$ cat text2
&
* * *
* * * * *
* * *
*
$ awk -f cmp.awk text1 text2
Symbol at Text 1 =  *
Symbol at Text 2 =  &
$

Jean-Pierre.
Hi Jean, it works!! Thanks.... but i hav some question about the codes...
array[1, x, y] = array[2, x, y] = " "; => is this a 3 D array since there are 3 indexes?

arr = (NR == FNR ? 1 : 2);

yy[arr] -= 1

=> what are these sentences trying to do ?

Thanks in advance
# 5  
Old 09-21-2006
Quote:
Originally Posted by Raynon
Hi Jean, it works!! Thanks.... but i hav some question about the codes...
array[1, x, y] = array[2, x, y] = " "; => is this a 3 D array since there are 3 indexes?

arr = (NR == FNR ? 1 : 2);

yy[arr] -= 1

=> what are these sentences trying to do ?

Thanks in advance

I reallise that when i add on another difference to Text 2 , the correct coordinate could not be captured any more.

Text 2:

&
* * *
* * ^ * *
* * *
*

Output:
$ nawk -f expert_awk text1 text2
Coordinates = (01,05)
Symbol at Text 1 = *
Symbol at Text 2 = &
Coordinates = (05,03) => wrong coordinate!!
Symbol at Text 1 = *
Symbol at Text 2 = ^

Can this script be used for any 5 by 5 matrix comparison or is this just limited to this particular orientation of the matrix ?
# 6  
Old 09-21-2006
My awk program considers single char values with no separator in the matrix.
I modify it for space separated values in the matrix (not limited to single caracters).
Code:
BEGIN {
   yy[1] = yy[2] = 5;
   for (x=1; x<=5; x++)
      for (y=1; y<=5; y++)
         array[1, x, y] = array[2, x, y] = " ";
}
FNR > 5 {
   next;
}
{
   arr = (NR == FNR ? 1 : 2);
   y   = yy[arr];
   $0  = $0 "     ";
   for (x=1; x<=5; x++) 
      array[arr, x, y] =$x;
   yy[arr] -= 1;
}
END {
   for (x=1; x<=5; x++)
      for (y=1; y<=5; y++)
         if (array[1, x, y] != array[2, x, y]) {
            printf("Coordinates = (%02d,%02d)\n", x, y);
            printf("Symbol at Text 1 =  %s\n", array[1, x, y]);
            printf("Symbol at Text 2 =  %s\n", array[2, x, y]);
         }
}

Now, some explanations :

array[1, x, y] = array[2, x, y] = " ";
array is a 3D array.
array{1] is for text1 and array[2] for text2.
This statement also can be written :
array[1, x, y] = " ";
array[2, x, y] = " ";



arr = (NR == FNR ? 1 : 2);
The ( test ? value1 : value2 ) syntax is the equiv to:
if ( test )
return value1
else
return value2


yy[arr] -= 1
a short cut for
yy[arr] = y[arr] - 1


Jean-Pierre.
# 7  
Old 09-21-2006
Here's an alternative in Python:

file1 :

05 *
04 * * *
03 ** * * *
02 * * *
01 *
0 0 0 0 0
1 2 3 4 5

file2:

05 &
04 * * *
03 ** * * *
02 # * $
01 *
0 0 0 0 0
1 2 3 4 5

Code:
text1 = open("file1.txt").readlines()
text2 = open("file2.txt").readlines()
for y in range(len(text1)): ## y-coordinates
	if text1[y] != text2[y]:
		t1_splitted  = text1[y].split()
		t2_splitted = text2[y].split()		
		for x in range(5): ## x-coordinates
			try:
				if t1_splitted[x] != t2_splitted[x]:					 
					coordinates = y,x #get coordinates
					print "Coordinates" , coordinates
					print "Symbol at text 1 %s" % t1_splitted[x] 
					print "Symbol at text 2 %s\n" % t2_splitted[x]
					print "-" * 50
			except:pass

Output:
Code:
/home> python test.py
Coordinates (0, 1)
Symbol at text 1 *
Symbol at text 2 &

--------------------------------------------------
Coordinates (3, 1)
Symbol at text 1 *
Symbol at text 2 #

--------------------------------------------------
Coordinates (3, 3)
Symbol at text 1 *
Symbol at text 2 $

--------------------------------------------------

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script with following awk command pls help

Hi I want to create a shell script with the following awk command & also get the filenames in output. awk '/<catetcsecuretty0>/ {p=1} /<catvarlogmessages0>/ {p=0} p' *.xml As there will be multiple outputs related to many xml files I cannot identify which output belongs to which file ... (5 Replies)
Discussion started by: sharp488
5 Replies

2. Solaris

Pls. help with Sun Array 6120

Hi, I am on Sun Edge 6120 Disk array. When I do port listmap. The failover status below means I have to take action. I never used Sun Array before. Please advice. port targetid addr_type lun volume owner access u1p1 1 hard 0 v0 ... (0 Replies)
Discussion started by: samnyc
0 Replies

3. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

4. Shell Programming and Scripting

use awk to edit a file..pls help

hey i want to over write the fourth field of a ':' delimited file by first finding the required row by using grep. i have done the following cat file | grep no. | awk -F ':' { $4=count; print $1:$2:$3:$4;} the correct values are being printed but nothin is bein added to the file..please... (5 Replies)
Discussion started by: dhe.arora
5 Replies

5. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

6. Shell Programming and Scripting

AWK - HELP pls explain this ?

echo "23.54" | awk ' function round(A) { return int( A + 0.5 ) } { printf("%d\n",round($1)); }'> > > > > > awk: syntax error near line 2 awk: bailing out near line 2 (2 Replies)
Discussion started by: santosh1234
2 Replies

7. UNIX for Dummies Questions & Answers

Pls help me with file editing using awk

Hi all, Pls help me with file editing using awk. I have a text file with the below format. "STANDARD VOLUME ","2009","BUX","V","JCBH49","NF", 001413 "VENDOR MATERIAL-STD ","2009","BUX","V","JCBH49","NF", 009948 "INBOUND TRANS-STD ... (2 Replies)
Discussion started by: srinivas.maddy
2 Replies

8. Shell Programming and Scripting

Rearrange array elements-Pls help

Hi, Do anyone of you know how to get this done.. consider I have an array array1 (a b c d e). Now based on some calculations I would get a No. which is the index of the above array.Say for example I get 3.Then, I would remove the third value from the array and rearrange the array as... (4 Replies)
Discussion started by: tj23
4 Replies

9. Shell Programming and Scripting

Help needed in 'awk' : pls.

we have a file File1.I have used the below code to append 'file1' to each row.it is appending 'file1' but there is 5 empty space is coming after the end of each row.i found this opening in 'vi' mode and moving the cursor on each row ...can u help me ... File1: E100|0|05/29/1993|0|E001|E000... (3 Replies)
Discussion started by: satyam_sat
3 Replies

10. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies
Login or Register to Ask a Question