AWK associative array problem
Following code runs well on a file where I know there are 3 fields:
Code:
gawk ' |
Help us to help you. Provide a sample input file (10-15 lines will do). Construct a sample output file which corresponds to your sample input and post both samples here. With "InFile" and "OutFile" examples we can better understand your needs and also judge if our proposed solution fills those needs.
Daniel B. Martin |
Indeed - as Daniel pointed out - a sample input/output would be useful to fully understand your requirement. Anyway, here is an example using multi-dimensional arrays:
Code:
$ cat file |
I should have provided an input file and output but I thought the problem was very clear. Thanks colucix, for the input file and the answer. This is exactly what I was looking for. I am now trying to understand how your elegant solution works.
The output data is sorted but it is so because of the way data is entered in input file. If the input file is: Code:
B1 B2 B3 B4 B5 Code:
Z1 : 1 |
Quote:
Daniel B. Martin |
Two-dimensional arrays are a powerful language feature but that kind of code can be difficult to write, difficult to read. At the expense of execution time you may read the input multiple times but enjoy the simplicity of one-dimensional arrays.
With this InFile ... Code:
B1 B2 B3 B4 B5 Code:
nc=$(awk '{if (NF>maxNF) maxNF=NF}; END{print maxNF}' $InFile) Code:
Z1 : 1 |
To sort arrays in awk you may use the asort function. Following my previous example you can change the last part into:
Code:
for ( i = 1; i <= length(c); i++ ) { |
Thanks for your reply. The solution looks elegant but it is producing some errors if there are blank fields. The programs that I tested is:
Code:
#! /bin/bash Code:
|
Ops, you right! Mine was a greenhorn's mistake: the array a retains the values of the last fields of the (previous) longer lines. Just add a delete statement at the end of the for loop:
Code:
for ( i = 1; i <= length(c); i++ ) { |
Yes, it works perfectly now. Thanks again.
|
I thought I would just throw 2 cents in ... colucix's solution is still using a 1 dimensional associative array. The SUBSEP delimited array could just as easily use any other character
not already in the values either side of the delimiter. However, as of v4, awk does indeed offer true multi-dimensional arrays. (See here for more details) Using a snippet from the script: Code:
# current associative array The gotcha here would be making sure you sort the second dimension and not the first ;) |
for following test file:
Code:
A1 A2 A3 A4 Code:
#!/bin/bash Code:
2 : B2 |
This solution uses one-dimensional arrays.
With this InFile ... Code:
A1 A2 A3 A4 Code:
awk '{line[NR]=$0" "; if (nc<NF) nc=NF}; Code:
A1 : 2 |
Thanks for your solution.
How can we use 2-dimensional array? I suspect it will be easier to understand. |
Quote:
Daniel B. Martin . |
All times are GMT -5. The time now is 01:08 PM. |