Visit Our UNIX and Linux User Community


Array usage issue with AWk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array usage issue with AWk
# 1  
Old 06-05-2011
Array usage issue with AWk

Hi friends,
I m trying to write small script in awk alone.
And I have tried below logic for one of my automation - taking the first column of the file in array
and will read in for loop for each time when i grep the array value in the same file
I should get complete output whichever is matching in which I would do some other checks.
but failing somewhere in my script,also never worked on reading the file twice in AWK Smilie
Quick help would be appreciated.

Code:
fileA
xxxxxx   567890  456789
kjlajl   3456789 489080
xxxxxx   klkio   467830

Code:
Output expected:
xxxxx "array executed"(from file after reading array one by one in for loop)
xxxxx  567890  456789
xxxxx   klkio   467830
kjlajl "array execcuted"(again from file after reading array)
kjlajl  3456789 489080

Code:
code I have tried:
$ nawk -F, 'NR==FNR{arr1[i++]=$1}
 {
for(i in arr1)
 {if($0~arr1[i]) print arr1[i]"executed""\n"$0}}' fileA

Thanks
Sha
# 2  
Old 06-05-2011
Since you are only reading one file, NR will always be the same as FNR so there is no reason to include that test as it's always true. Secondly, it's never good form to use a variable, i in this case, for loop control when it is used outside of the loop with the assumption that it has some previously sane value.

Here is an example that illustrates two methods, one if output order must match the same order that field 1 was seen, and one if any output order is ok.

Code:
# if order is important
awk '
    {
        if( !seen[$1]++ )               # track order that field 1 was observed
            order[oidx++] = $1;

        map[$1,idx[$1]++]= $0;          # save each line based on the first field
    }
    END {
        for( o = 0; o < oidx; o++ )      # for each field 1 in the order seen
        {
            printf( "executed: %s\n", order[o] );
            for( m = 0; m < idx[order[o]]; m++ )   # for each line associated with field 1 value
                printf( "%s\n", map[order[o],m] );

        }
    }
' testfile

Code:
# if output order isn't important
awk '
    {
        map[$1,idx[$1]++]= $0;          # save each line based on the first field
    }
    END {
        for( i in idx )              # for each field 1 seen (any order)
        {
           printf( "executed: %s\n", i );
           for( m = 0; m < idx[i]; m++ )   # for each line associated with this field 1
               printf( "%s\n", map[i,m] );
        }
    }
' testfile

I just noticed you're using nawk, you can replace awk with nawk in the examples and you should be fine.

Last edited by agama; 06-05-2011 at 06:54 PM.. Reason: clarification
This User Gave Thanks to agama For This Post:
# 3  
Old 06-05-2011
Thanks a lot for the detailed Answer aagama!!

It worked Smilie
# 4  
Old 06-05-2011
Code:
sort infile |awk '!a[$1]++ {print "executed: " $1}1'


Previous Thread | Next Thread
Test Your Knowledge in Computers #845
Difficulty: Medium
Sideloading is a term used mostly on the Internet, similar to "upload" and "download", but in reference to the process of transferring files between two local devices, in particular between a computer and a mobile device such as a mobile phone,
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk array next issue

Hi, Here's an example of what I my issue, a file list.txt, which looks like: a b c d e I have been trying to use the following code to simply print the list using awk and next: awk 'FNR==NR{a=$1;next}{print a}' list.txt this give no output, even though I would expect it to type out... (5 Replies)
Discussion started by: hexy
5 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. UNIX for Dummies Questions & Answers

Variable usage issue

Hi, I need a help in setting scope of the variable. I want to use the below logic right before the "break" statement if ; then echo $header echo $trailer fi But due the scope of the variable it is causing issues. I tried using "export" statement. But it changes the output completely ... (0 Replies)
Discussion started by: Prem148
0 Replies

4. Shell Programming and Scripting

Scripting array issue

Hi guys, I'm a scripting noob in need of some help :) I am creating a script that checks the filesystems and will alert based upon if the percent full is greater than the threshold set. The problem that I am having is that when I set the 'filesystem' variable, all of the output is treated as... (12 Replies)
Discussion started by: tank126
12 Replies

5. UNIX Desktop Questions & Answers

Issue with disk space usage

Issue with disk space usage I have the following line in my "df -h" output: Filesystem Size Used Avail Capacity Mounted on /dev/ad4s1a 496M 495M -39M 109% / What is the issue with having 9% excess utilisation? How can I find out what this partition is... (2 Replies)
Discussion started by: figaro
2 Replies

6. Shell Programming and Scripting

Perl Array / pattern match large CPU usage

Hi, I have one file in this format 20 value1 33 value2 56 value3 I have another file in this format: 34,30-SEP-09,57,100237775,33614510126,2,34 34,30-SEP-09,57,100237775,33620766654,2,34 34,30-SEP-09,108,100237775,33628458122,2,34 34,30-SEP-09,130,100237775,33635266741,2,254... (6 Replies)
Discussion started by: Donkey25
6 Replies

7. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

8. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

9. UNIX for Dummies Questions & Answers

Array usage

#!/bin/ksh let i=0 set -A array array{1}="qqqqqqqqqqqqqqqqqqq" when run got error t1: array{1}=qqqqqqqqqqqqqqqqqqq: not found Whats wrong here Thanks in advance (2 Replies)
Discussion started by: zam
2 Replies

10. Solaris

V880 and T3 array issue

We're trying to install a third T3 array onto our V880. The other two T3's are connected to a qlogic fibre card. We can see this connection fine when we do a luxadm probe -p command. The full device paths are shown etc. ie: the first T3's logical name is c2t1d0 and the second one is c3t1d0. ... (3 Replies)
Discussion started by: mjl927
3 Replies

Featured Tech Videos