Sponsored Content
Top Forums Shell Programming and Scripting Help With Array Operations in AWK Post 302420015 by cold_Que on Monday 10th of May 2010 10:51:14 AM
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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
ZDIFF(1)						      General Commands Manual							  ZDIFF(1)

NAME
zcmp, zdiff - compare compressed files SYNOPSIS
zcmp [ cmp_options ] file1 [ file2 ] zdiff [ diff_options ] file1 [ file2 ] DESCRIPTION
Zcmp and zdiff are used to invoke the cmp or the diff program on files compressed via gzip. All options specified are passed directly to cmp or diff. If only file1 is specified, it is compared to the uncompressed contents of file1.gz. If two files are specified, their con- tents (uncompressed if necessary) are fed to cmp or diff. The input files are not modified. The exit status from cmp or diff is pre- served. SEE ALSO
cmp(1), diff(1), zmore(1), zgrep(1), znew(1), zforce(1), gzip(1), gzexe(1) BUGS
Messages from the cmp or diff programs may refer to file names such as "-" instead of to the file names specified. ZDIFF(1)
All times are GMT -4. The time now is 06:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy