[CentOS] Semi-OT: awk

J Martin Rushton martinrushton56 at btinternet.com
Fri Apr 29 22:27:58 UTC 2016


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



On 29/04/16 18:14, Jon LaBadie wrote:
> On Fri, Apr 29, 2016 at 10:31:51AM -0400, m.roth at 5-cent.us wrote:
>> This is odd, and annoying. CentOS 6, current. Here's my awk
>> script:
>> 
>> { room = substr($0, 48, 10); arr[$2,room,$1] = $0; } END { for (
>> i in arr ) { for ( j in arr[i] ) { for ( k in arr[i][j] ) { print
>> arr[i][j][k]; } } } }
>> 
> 
> Note the last sentence in this paragraph from the gawk manpage:
> 
> The in construct may also be used in a for loop to iterate over all
> the elements of an array.  However, the (i, j) in array construct
> only works in tests, not in for loops.
> 
> jl
> 
I modelled your problem, just changing line 2 to "room = $3".  The
data file was:
1 2 3
4 5 6
7 8 9

The problem is in the line ( i in arr ).  i will be "1SUBSEP2SUBSEP3",
which makes a nonsense of the rest of the code.

Using

END {
  for ( a in arr ) {
     split( a, a2, SUBSEP )
     print a2[1], a2[2], a2[3]
   }
}

I get a sensible output.  You'll have to take it from here.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)

iQIcBAEBAgAGBQJXI9/uAAoJEAF3yXsqtyBlgu4QALCdTlhv+oiVpYyQsksJKSUP
WOj/xIZGxzFSQM/TeXPozqr39q2fJUSGQkpxbzHZL72t32z3CS0GZPN4d1ahlk43
v68GTzDx5FgXlZXAWaSAe0+w6e8kYr0Vaf5IPmpQttSL1iwHGLLmGdK9P4s+6kRn
GroNnY8ULj7yy4ruXF9Axas4MV//Cj9NkIenPHnR+inMk0LPQFM7wQqTbPPCGn2x
WqAjaIAumHs9VwNNZq4xyi57tBK2CYajCmseeZof9EJVb8oKRoSjw00wZqrTHBM4
K5ICDNcCVeH9uNWNGfV/z8ezw9lUL4jIzgf6WObXx4ALRIPAHMLthT/OUNvTCLNM
JImeIpKmK+9cy7T3EmDy72IDhUx351Bj3bI3nGyLgaRpEmWVo8YA4VMwWRojGP70
Hq9RhC8bv8CW87DmZcZesvTmD/pP4XiuTURWuock3fpauupZUqbJ3V88SittU2pw
7jWvnYbqHsPk2Oc9J+uRwJec3J60jGE82s13OXsfiw35XA/pyA+YlBPWQbNLHGS1
2BDxDd/KsOo7vjwFYk3tRgHqDfCFJyo5wT6jwMoejguvbLxgTQc/la8izAFMAzLy
aZ3q12mXiejvTKBI22RS8VMl+N/MUzUth9roROArG84/nynDj3IEVFYYFKQwZgxX
cnfr0Sjcykioytl12jFR
=O4BS
-----END PGP SIGNATURE-----



More information about the CentOS mailing list