Sponsored Content
Top Forums Shell Programming and Scripting Awk: What is the difference between: X[a,b,c] - X[a][b,c] - X[a][b][c] Post 303001498 by Fustbariclation on Saturday 5th of August 2017 05:07:50 AM
Old 08-05-2017
Wrench Awk: What is the difference between: X[a,b,c] - X[a][b,c] - X[a][b][c]

I have awk appearing to behave inconsistently. With the same variable it will give the message:

Code:
fatal: attempt to use array `X["2"]' in a scalar context

and, if I try to correct that, then:

Code:
fatal: attempt to use a scalar value as array

I'm using a three dimensional array. There seems to be a significant difference to how awk - GNU Awk 4.1.4, API: 1.1 (GNU MPFR 3.1.5, GNU MP 6.1.2) - treats the following:

Code:
X[a,b,c]
X[a][b,c]
X[a][b][c]

What is the difference between these? Where can I find it documented?

To demonstrate, with a small piece of code:

Code:
BEGIN {
        X[1,"Jim","The quick brown fox jumps over the lazy dogs"]++;
        Y[1]["Jim","The quick brown fox jumps over the lazy dogs"]++;
        Z[1]["Jim"]["The quick brown fox jumps over the lazy dogs"]++;
        X[2,"Jack","The quick brown foxs jumps over the lazy dogs"]++;
        Y[2]["Jack","The quick brown foxs jumps over the lazy dogs"]++;
        Z[2]["Jack"]["The quick brown foxs jumps over the lazy dogs"]++;
        X[3,"Joe","The quick brown foxs jumps over the lazy dogs"]++;
        Y[3]["Joe","The quick brown foxs jumps over the lazy dogs"]++;
        Z[3]["Joe"]["The quick brown foxs jumps over the lazy dogs"]++;
      }
END {
for ( x in X )
        print x;
for ( y in Y )
        print y;
for ( z in Z )
        {
        for ( a in Z[z] )
                print z " : " a  # -> gives the error 'awk: demo.awk:20: fatal: attempt to use array `Z["1"]["Jim"]' in a scalar context' ":" Z[z][a];
        }
}

$ awk -f demo.awk </dev/null

Code:
1JimThe quick brown fox jumps over the lazy dogs
2JackThe quick brown foxs jumps over the lazy dogs
3JoeThe quick brown foxs jumps over the lazy dogs
1
2
3
1 : Jim
2 : Jack
3 : Joe

As the code says, in the last example, if I try
Code:
Z[z][a]

which seems the correct syntax, it gives the error
Code:
fatal: attempt to use array `Z["1"]["Jim"]' in a scalar context

.

Code:
split(Y[1],y_bits,SUBSEP);

gives:
Code:
awk: demo.awk:14: fatal: attempt to use array `Y["1"]' in a scalar context

The same error occurs for
Code:
split(Z[1],z_bits,SUBSEP);

but
Code:
split(X[1],x_bits,SUBSEP);

works.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk help in calulating difference for last two lines

Hi all, I have a requirement for a script to find out the increase in memory. We have a log native_stderr.log where this will log. bash-2.05$ tail -40 native_stderr.log | grep ': freed' <GC(4140): freed 168190456 bytes, 66% free (180990488/271776256), in 253 ms> <GC(4141): freed... (4 Replies)
Discussion started by: senthilkumar_ak
4 Replies

2. Shell Programming and Scripting

AWK - Difference in multiple files

Hello again, I've run into another problem that I've been unable to solve. With everyone's help last time, the script worked perfectly! This problem takes a little more finesse, and the bash script I thought up didn't work, so I've canned it. I'd like to try awk if possible. Here's my... (6 Replies)
Discussion started by: Eblue562
6 Replies

3. Shell Programming and Scripting

AWK Script and Commandline difference

Hey there, I just stumbled upon a difference between using awk on the commandline and using it in a shellscript. I have a variable, e.g.: PROG=vim then i want to check if the package with this name is installed: TEMPVAL=$(dpkg -l | awk '{ if ($2 == "$PROG") print $2 }') (Im using... (10 Replies)
Discussion started by: MrSnail
10 Replies

4. Shell Programming and Scripting

[Solved] awk Column difference

Hi, I've got what is probably quite an easy little (presumably) awk problem that I just can't seem to work out (mental block...I've already spent ages getting the data into this format!). I want to work out the difference between rows for certain columns. for example: 1359142876 RED 14... (3 Replies)
Discussion started by: chrissycc
3 Replies

5. Shell Programming and Scripting

awk -- telling the difference between strings and integers

This should be a really easy question. My input file will have a few fields that are strings in the first line, which I will extract and save as variables. The rest of the fields on every line will be integers and floating point numbers. Can awk tell the difference somehow? That is, is there... (5 Replies)
Discussion started by: Parrakarry
5 Replies

6. UNIX for Dummies Questions & Answers

Shell script - getting Time difference using awk

Hi..I have the data in a file like in this format, and I need the output time difference in seconds by using awk command. Start date/time and end date/time given in column 2,3 & 4,5. Please assist how to write shell script. File1.txt JOB1 10/09/2013 17:42:16 10/09/2013 17:43:46 SU 6202685/1... (4 Replies)
Discussion started by: mprithvi
4 Replies

7. UNIX for Dummies Questions & Answers

What is the difference in this two awk command?

What is the difference in these two awk command? Both returns same output but I am not sure what is the use of +0 in command 1. awk -F "," '{print $1+0,$2+0,$3+0}' awk -F "," '{print $1, $2, $3}' (3 Replies)
Discussion started by: later_troy
3 Replies

8. Shell Programming and Scripting

Difference in awk output and while

so, im going over one of my scripts and trying to optimize it. i have a code like this: cksum sjreas.py | awk '{prinnt $1$2}' This does what I need. However, i dont want to call the external command awk. so im doing this: cksum sjreas.py | while OFS=' ' read v1 v2 ; do printf... (4 Replies)
Discussion started by: SkySmart
4 Replies

9. Shell Programming and Scripting

awk to calculate difference of split and sum the difference

In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13. I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Discussion started by: cmccabe
2 Replies

10. UNIX for Beginners Questions & Answers

awk to capture memory difference

Hi, I have a file which consists of the following information in repeating blocks. ************First iteration*************** xr_lab#show memory compare start Thu Sep 19 14:38:06.400 WIB <<<<<<<<<< START TIME Successfully stored memory snapshot in... (3 Replies)
Discussion started by: sand1234
3 Replies
All times are GMT -4. The time now is 03:52 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy