retaining awk array in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting retaining awk array in shell
# 1  
Old 06-26-2010
retaining awk array in shell

Is there a way to retain the awk array in the shell.

My requirement

file a.txt
Code:
A       B        Count
10      1        25
10      2        20
10      3        21
11      1        20
11      2        22
12      2        40
12      3        15

A and B are my variables and count is generated with associative array
this line of code is generating this file
Code:
{ arr[$4"\t"$5]++ ; }

Now the problem is this file "a.txt" is generated every hour and depending on the user input I might need to generate a consolidated report for hours.

so precisely if another "a.txt" contains an entry
Code:
A       B        Count
10      1        40

then my report output should print the output as
Code:
A       B        Count
10      1        65

(which is consolidation from a.txt and b.txt)

Is there any way this can be achieved.

regards
Jagpreet

Last edited by Scott; 06-26-2010 at 07:46 AM.. Reason: Please use code tags
# 2  
Old 06-26-2010
Hi
Do you mean to say you get multiple files of the format a.txt, and you are looking for a way to get the cumulative result? It will be more helpful if you could specify the original input file contents and exact expected result.

Guru.
# 3  
Old 06-26-2010
Hi Guru,
ya its multiple a.txt files every hour
e.g.
Code:
a_2010062400.txt
a_2010052401.txt

I need the cumulative output

e.g. a_2010062400.txt contains

Code:
128655  39 28
127655  96 28
127655  94 28
127655  82 28
112655  173 28
109655  206 28
105655  55 28
85655   35 27
81655   200 27
79655   476 27
69655   392 27
69655   182 27
53655   170 27
48655   30 27
46655   65 27
45655   9 27

e.g. a_2010062400.txt contains
Code:
69655   392 57
69655   182 57
53655   170 57
48655   30 57
46655   65 57
45655   9 57

the output should be

Code:
128655  39 28
127655  96 28
127655  94 28
127655  82 28
112655  173 28
109655  206 28
105655  55 28
85655   35 27
81655   200 27
79655   476 27
69655   392 84
69655   182 84
53655   170 84
48655   30 84
46655   65 84
45655   9 84


Hope its clearer now

Last edited by Scott; 06-26-2010 at 07:47 AM.. Reason: Code tags
# 4  
Old 06-26-2010
Hi

Code:
awk '{a[$1"  "$2]+=$3;}END{for(i in a) print i,a[i];}' a_2010062400.txt a_2010052401.txt

Guru.
# 5  
Old 06-26-2010
Its working.

Thanks Guru,

Its working.
Its just, the output is dependent on user user input for cumulative report or hourly report but a single "if" did the trick for me.

I was just wondering if I can print the output in a nice format.
I am using
Code:
       awk '
       {
         arr[$1"\t\t\t"$2]+=$3;
       } END {
       for(i in arr) printf("%25s %25d\n",i,arr[i]);
       }' $filename

but still its not looking as good as I want. can "\t\t\t" be replaced with something better.

Last edited by Scott; 06-26-2010 at 07:48 AM.. Reason: Code tags
# 6  
Old 06-26-2010
Hi

Try this for better formatting:

Code:
awk '{a[$1" "$2]+=$3;j++}END{for(i in a){split(i,b," "); printf"%-8d %-4d %-4d\n", b[1],b[2],a[i];}}' a_2010062400.txt a_2010052401.txt

Guru.
# 7  
Old 06-28-2010
Code:
nawk '{
        a[$1" "$2] += $3
} END {
        for ( i in a ){
                split(i ,b ," ")
                printf("%-10d%-6d%-6d\n", b[1], b[2], a[i])
        }
} ' a_2010062400.txt a_2010062400.txt  | sort -k 1n | tail -r


Last edited by radoulov; 06-28-2010 at 05:09 PM.. Reason: Code tags, please!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
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

Problem with awk array when loading from shell variable

Hi, I have a problem with awk array when iam trying to use awk in solaris box as below..Iam unable to figure out the problem.. Need your help. is there any alternative to make it in arrays from variable values nawk 'BEGIN {SUBSEP=" "; split("101880|110045 101887|110045 101896|110045... (9 Replies)
Discussion started by: cskumar
9 Replies

4. Shell Programming and Scripting

Pass awk array variable to shell

Hi, all suppose I have following myfile (delimited by tab) aa bb cc dd ee ffand I have following awk command: awk 'BEGIN{FS="\t"}{AwkArrayVar_1=$1;AwkArrayVar_2=$2};END{for(i=0; i<NR; i++) print i, AwkArrayVar_1, AwkArrayVar_2,}' myfileMy question is: how can I assign the awk array... (7 Replies)
Discussion started by: littlewenwen
7 Replies

5. Shell Programming and Scripting

Assigning array values using awk in shell scripting

hi My script as below #!/bin/ksh for i in `seq 1 7` do a=$(awk '{print $i}' /home/rama/expenese.txt) done for i in `seq 1 7` do echo "${a}" done content of expense.txt is as below 5032 210179 3110 132813874 53488966 11459221 5300794 I want output as... (6 Replies)
Discussion started by: Ramakrishna V
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

saving values from awk expression into shell array

hi i am trying to save the values i extract from a file with the help of awk in a bash shell array. i have: exec 10<file2 while read LINE <&10; do ARRAY1=$(awk '{print $1}' file2) ((count++)) done echo ${ARRAY1} it prints just blank lines. file1 has two columns and i... (4 Replies)
Discussion started by: npatwardhan
4 Replies

8. Shell Programming and Scripting

Pass array variabel to awk from shell

Hi I need to pass an array to Awk script from Shell. Can you please tell how to do it? How to pass this array add_ct_arr to an awk script or access it in awk? i=1 while ; do add_ct_arr=$(echo ${adda_count} | awk -v i=$i -F" " '{print $i;}') echo ${add_ct_arr} ... (1 Reply)
Discussion started by: appsguy616
1 Replies

9. Shell Programming and Scripting

Accessing awk array from shell

Hi, i want awk to read a file and place it's content into two arrays. When trying to read these arrays with a "for a in ${source_path} "-Loop it gives the right result. But when trying to access directly (/bin/echo ${source_path}) it doesn't work. I read "all "the awk threads in this forum and... (6 Replies)
Discussion started by: bateman23
6 Replies

10. Shell Programming and Scripting

Could someone give me an example of awk accessing array defined in Korn Shell?

As per title and much apprecieated! (2 Replies)
Discussion started by: biglau
2 Replies
Login or Register to Ask a Question