Help With Array Operations in AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help With Array Operations in AWK
# 1  
Old 05-10-2010
Help With Array Operations in AWK

I have two files

file1
[
Code:
            A 2 4 6 8
            B 1 3 5 7
            C 1 3 5 7
            D 1 3 5 7
            E 1 3 5 7

file2
Code:
             C D E F G
           H I J  K L

I need to add field 1 of file1 to both field 2s of file2 and repeat the same on all the rows in file 1...... so the output should be

A+D
A+I
B+D
B+I.......ect

The code below is what I have, but there has to be a better way. You see I have to repeat the same command 5 times to account for the 5 rows in file1

Where: CONG[i,j] is a multideminsonal array of file1 and SA[i,j] is a multi-dimensional array of file2


Code:
awk ' { for (j=1;j<=NF;j++) {
         CONG[NR,j]=$j
                             }
        {getline < "'$SA'"}  { for (j=1;j<=NF;j++) {
                                 SA[NR,j]=$j      }
                             }
       {for (i=1;i<=NR;i++) {print SA[i,1],SA[i,2],CONG[1,2],CONG[1,5],CONG[1,5] + SA[i,2]}}
       {for (i=1;i<=NR;i++) {print SA[i,1],SA[i,2],CONG[2,2],CONG[2,5],CONG[2,5] + SA[i,2]}}
       {for (i=1;i<=NR;i++) {print SA[i,1],SA[i,2],CONG[3,2],CONG[3,5],CONG[3,5] + SA[i,2]}}
       {for (i=1;i<=NR;i++) {print SA[i,1],SA[i,2],CONG[4,2],CONG[4,5],CONG[4,5] + SA[i,2]}}
       {for (i=1;i<=NR;i++) {print SA[i,1],SA[i,2],CONG[5,2],CONG[5,5],CONG[5,5] + SA[i,2]}}
      }' | sort -u |

Thanks
# 2  
Old 05-10-2010
Don't quite understand the desired output, but... something to start with:
Code:
nawk '
FNR==NR {f1[FNR]=$2;next}
{ for(i=1; i in f1; i++) print $1 "+" f1[i]}' file2 file1

# 3  
Old 05-10-2010
Thanks vgersh9, I'll try this out.

In short, I want to add/subtract the first field in file1 with every field 2 in file2, and repeat the process for every row in file1

A+D
A+I
B+D
B+I
C+D
C+I
D+D
D+I
E+D
E+I

---------- Post updated at 11:26 AM ---------- Previous update was at 10:35 AM ----------

Thanks code worked well
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

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

3. Shell Programming and Scripting

awk - sed / reading from a data file and doing algebraic operations

Hi everyone, I am trying to write a bash script which reads a data file and does some algebraic operations. here is the structure of data.xml file that I have; 1 <data> 2 . 3 . 4 . 5 </data> 6 <data> 7 . 8 . 9 . 10</data> etc. Each data block contains same number of lines (say... (4 Replies)
Discussion started by: hayreter
4 Replies

4. Shell Programming and Scripting

Array from awk

I'm trying to get the output from awk into a bash array. Here is my script. #!/bin/bash while : do app=$( osascript -e "tell application \"System Events\" to return name of every process whose frontmost is true" ) echo "$app" if ava ]] then ps -ax | grep -v awk | pids=( $(awk... (5 Replies)
Discussion started by: nextyoyoma
5 Replies

5. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

6. Shell Programming and Scripting

Arthmetic & Operations Using AWK

Trying to do math while using awk and if statements. awk 'BEGIN {FS = ":"} ; {if($2=1) {$3 +=}; print "Total employee work hours: " $3}' works_on.txt The file format is 123456789:1:32.5 123456789:2:7.5 666884444:3:40.0 453453453:1:20.0 453453453:2:20.0 333445555:2:10.0 ... (6 Replies)
Discussion started by: Rapcher
6 Replies

7. Shell Programming and Scripting

sh array and awk help...

Hi, I'm newbie with shell scripting and I'm in trouble with this: prenotazioni=/root/script/dhcp/prenotazioni.txt file=/root/script/dhcp/listaage.txt for nomefile in $(cut -d: -f1 $file); do ind=3 while do comando="$"$ind option=$(grep $nomefile $prenotazioni | awk... (2 Replies)
Discussion started by: gua76
2 Replies

8. UNIX for Advanced & Expert Users

Array Operations in tcl

I would like to create an array using tcl script which takes in name, id, and marks of many people in 3 subjects. I would like calculate the total marks of each person and rank them according to their marks. Taking name as my key for access i want to do sort and rank the person based on highest... (3 Replies)
Discussion started by: Carlton
3 Replies

9. Shell Programming and Scripting

awk command: column operations

I have a txt file with two columns Freq Loss 10 30 20 40 30 10 40 50 50 60i have used the below code to get the minimum value out of this array in second cloumn. awk 'NR==N{min=$N;max=$N}NR>N{if ($N>max){max=$N};if ($N<min){min=$N}}END {print... (3 Replies)
Discussion started by: shashi792
3 Replies

10. Shell Programming and Scripting

AWK help. how to compare a variable with a data array in AWK?

Hi all, i have a data array as follows. array=ertfgj2345 array=456ttygkd . . . array=errdjt3235 so number or elements in the array can varies depending on how big the data input is. now i have a variable, and it is $1 (there are $2, $3 and so on, i am only interested in $1). ... (9 Replies)
Discussion started by: usustarr
9 Replies
Login or Register to Ask a Question