Sponsored Content
Top Forums UNIX for Dummies Questions & Answers awk: syntax for "if (array doesn't contain a particular index)" Post 302703487 by agama on Wednesday 19th of September 2012 10:01:46 PM
Old 09-19-2012
You are getting the extra X at the end because you are executing your loop one time more than you have pairs in your string. This is the way I would have coded it:

Code:
awk '
    BEGIN {
        xtab["11"]="a";
        xtab["12"]="b";
        xtab["13"]="c";
    }
    function xlate( w,      xw, i, ss )
    {
        xw = "";
        for( i = 1; i < length( w ); i+= 2 )
        {
            ss = substr( w, i, 2 );
            xw = xw (ss in xtab ? xtab[ss] : "x");
        }

        return xw
    }
    { print xlate( $1 ); }
'

There isn't any bound checking to see that the word passed in is an even number of characters. You could also make the function accept both starting and ending positions in the word in addition to the word.

In my opinion, it's bad form to reference field values (e.g. $1) in a function. Better to pass in the contents of the field that you wish to operate on.

If you want to use a while loop (more cumbersome), then you'll need to 'look ahead' before looping. Something like this:

Code:
awk '
BEGIN {
        xtab["11"]="a";
        xtab["12"]="b";
        xtab["13"]="c";
    }
  
    # accepts word and starting offset 
    function xlate( w, start,       xw, n )
    {
        xw = ""
        n = substr(w, start, 2)    # seed n with first pair 
        while( n != "" )
        {
            xw = xw (n in xtab ? xtab[n] : "X")
            start = start + 2;
            n = substr(w, start, 2)    # look ahead to next; exit loop if at end
        }
        return xw
    }
    { print xlate($1, 1) }
'

Hope this gets you going again.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk Syntax Error "!~"

This is my Awk command. direct_count=`awk -F; 'BEGIN { direct_count=0 } $16 !~ /(PBLON|PBNY)/ || $18 !~ /(PBLON|PBNY)/ { direct_count++ } END { print direct_count }' ls.txt` I get a syntax error. BEGIN { direct_count=0 } $16 !~ /(PBLON|PBNY)/ || $18 !~ /(PBLON|PBNY)/ { direct_count++ }... (6 Replies)
Discussion started by: yongho
6 Replies

2. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

3. Shell Programming and Scripting

acessing awk array element while getline < "file"

I am attempting to write a awk script that reads in a file after awk array elements are assigned and using those elements while reading in the new file. Does this make sense? /pattern/ {tst=$3} (( getline < "file" ) > 0 ) { x=x " "tst } When I print tst in the END statement it... (9 Replies)
Discussion started by: timj123
9 Replies

4. Shell Programming and Scripting

awk array starting with "ord"

I was writing a awk function and had a error I was wondering about. It revolves around a Multidimensional array starting with ord example: if ( _e == 5 ) { lmrb=$5 ; lmtb=$6 ; larb=$7 ; latb=$8 } ... (2 Replies)
Discussion started by: timj123
2 Replies

5. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

6. Shell Programming and Scripting

Need to parse file "x" lines at a time ... awk array?

I have files that store multiple data points for the same device "vertically" and include multiple devices. It repeats a consistant pattern of lines where for each line: Column 1 is a common number for the entire file and all devices in that file Column 2 is a unique device number Column 3 is... (7 Replies)
Discussion started by: STN
7 Replies

7. Programming

C: Initialize "const" array from the "heap"

Hello, I am working on solving an NP-Complete problem, so it is very important that operations and data with limited integer-argument ranges be computed using immutable look-up-tables contained entirely in CPU cache. Retrieval of the look-up-table data must never leave the CPU once initially... (6 Replies)
Discussion started by: HeavyJ
6 Replies

8. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

9. HP-UX

DLT 7000 tape drive failing with "write error on a record in the index"

HP rp5450 (L2000) running HP-UX 11.11B Using DLT 7000 and DLT 4000 tape drives for nightly full backups Backup jobs created by SAM DLT 7000 cron entry is as follows: 00 2 * * 1-6 /usr/sam/lbin/br_backup DLT FULL Y /dev/rmt/0m /var/sam/graphLCAa17036 root Y 1 N > /var/sam/SAM_br_msgs 2>&1... (1 Reply)
Discussion started by: dreh99
1 Replies

10. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies
RQUOTAD(8)						      System Manager's Manual							RQUOTAD(8)

NAME
rpc.rquotad - remote quota server SYNOPSIS
/usr/sbin/rpc.rquotad [ -FI ] [ -p port ] [ -s | -S ] [ -x path ] /usr/sbin/rpc.rquotad [ -h | -V ] DESCRIPTION
rpc.rquotad is an rpc(3) server which returns quotas for a user of a local filesystem which is mounted by a remote machine over the NFS. It also allows setting of quotas on NFS mounted filesystem (if configured during compilation and allowed by a command line option -S). The results are used by quota(1) to display user quotas for remote filesystems and by edquota(8) to set quotas on remote filesystems. rquotad daemon uses tcp-wrappers library (under service name rquotad) which allows you to specify hosts allowed/disallowed to use the daemon (see hosts.allow(5) manpage for more information). The rquotad daemon is normally started at boot time from the system startup scripts. OPTIONS
-h, --help Show program usage and exit. -V, --version Show version of quota tools. -s, --no-setquota Don't allow setting of quotas (default). This option is available only if utilities were compiled with the rpcsetquota option. -S, --setquota Allow setting of quotas. This option is available only if utilities were compiled with the rpcsetquota option. -F, --foreground Run daemon in foreground (may be useful for debugging purposes). -I, --autofs Do not ignore autofs mountpoints. -p port, --port port Listen on alternate port port. -x path, --xtab path Set an alternative file with NFSD export table. This file is used to determine pseudoroot of NFSv4 exports. The pseudoroot is then prepended to each relative path (i.e. a path not beginning by '/') received in a quota RPC request. FILES
aquota.user or aquota.group quota file at the filesystem root (version 2 quota, non-XFS filesystems) quota.user or quota.group quota file at the filesystem root (version 1 quota, non-XFS filesystems) /etc/mtab default filesystems SEE ALSO
quota(1), rpc(3), nfs(5), services(5), inetd(8) RQUOTAD(8)
All times are GMT -4. The time now is 08:49 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy