Sponsored Content
Full Discussion: Array usage issue with AWk
Top Forums Shell Programming and Scripting Array usage issue with AWk Post 302527816 by agama on Sunday 5th of June 2011 04:52:21 PM
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 05:54 PM.. Reason: clarification
This User Gave Thanks to agama For This Post:
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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 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
XkbKeyAction(3) 						   XKB FUNCTIONS						   XkbKeyAction(3)

NAME
XkbKeyAction - Returns the key action SYNOPSIS
XkbAction XkbKeyAction ( xkb, keycode, idx ) XkbDescPtr xkb; KeyCode keycode; int idx; ARGUMENTS
- xkb Xkb description of interest - keycode keycode of interest - idx index for group and shift level DESCRIPTION
A key action defines the effect key presses and releases have on the internal state of the server. For example, the expected key action associated with pressing the Shift key is to set the Shift modifier. There is zero or one key action associated with each keysym bound to each key. Just as the entire list of key symbols for the keyboard mapping is held in the syms field of the client map, the entire list of key actions for the keyboard mapping is held in the acts array of the server map. The total size of acts is specified by size_acts, and the number of entries is specified by num_acts. The key_acts array, indexed by keycode, describes the actions associated with a key. The key_acts array has min_key_code unused entries at the start to allow direct indexing using a keycode. If a key_acts entry is zero, it means the key does not have any actions associated with it. If an entry is not zero, the entry represents an index into the acts field of the server map, much as the offset field of a KeySymMapRec structure is an index into the syms field of the client map. The reason the acts field is a linear list of XkbActions is to reduce the memory consumption associated with a keymap. Because Xkb allows individual keys to have multiple shift levels and a different number of groups per key, a single two-dimensional array of KeySyms would potentially be very large and sparse. Instead, Xkb provides a small two-dimensional array of XkbActions for each key. To store all of these individual arrays, Xkb concatenates each array together in the acts field of the server map. The key action structures consist only of fields of type char or unsigned char. This is done to optimize data transfer when the server sends bytes over the wire. If the fields are anything but bytes, the server has to sift through all of the actions and swap any nonbyte fields. Because they consist of nothing but bytes, it can just copy them out. XkbKeyAction returns the key action indexed by idx in the two-dimensional array of key actions associated with the key corresponding to keycode.idx may be computed from the group and shift level of interest as follows: idx = group_index * key_width + shift_level STRUCTURES
The KeySymMapRec structure is defined as follows: #define XkbNumKbdGroups 4 #define XkbMaxKbdGroup (XkbNumKbdGroups-1) typedef struct { /* map to keysyms for a single keycode */ unsigned char kt_index[XkbNumKbdGroups]; /* key type index for each group */ unsigned char group_info; /* # of groups and out of range group handling */ unsigned char width; /* max # of shift levels for key */ unsigned short offset; /* index to keysym table in syms array */ } XkbSymMapRec, *XkbSymMapPtr; X Version 11 libX11 1.2.1 XkbKeyAction(3)
All times are GMT -4. The time now is 12:30 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy