Search pattern and print hits lower than threshold - arrays

Here is an example what I need:
INPUT:
a 5
a 7
a 11
b 10
b 11
b 12
.
.
.
OUTPUT:
a 2
b 0
So on output should be hits lower than my threshold (in this case it is $2 < 10).
My code is:
awk 'OFS="\t" {v[$1]+=$2; n[$1]++} END {for (l in n) {print l, n[l]} }' input
and my output is
a 3
b 3
I am not sure where to put condition $2 < 10.

You can check the threshold condition with something like $2 < value, where value is an awk variable given with -v value=XX.
Also, you are using v[$1]+=$2: this sums, not counts the matching cases.
All together, I would use this:
awk -v t=10 '{list[$1]} $2<t {count[$1]++} END {for (i in list) print i, count[i]+0}' file
Note we need to use two arrays: one to keep track of the counters and another one the keep track of all possible values.
Explanation
-v t=10 provide threshold.
{list[$1]} keep track of all possible first fields appearing.
$2<t {count[$1]++} if the 2nd field is smaller than the threshold, increment the counter.
END {for (i in list) print i, count[i]+0} finally, loop through all the first fields and print the number of times they had a value lower than the threshold. The count[i]+0 trick makes it print 0 if the value is not set.
Test
$ awk -v t=10 '{list[$1]} $2<t {count[$1]++} END {for (i in list) print i, count[i]+0}' a
a 2
b 0

Related

How to access even elements of array in bash

I want to echo the even elements of an array in bash, how could this be achieved?
Assuming your array is not sparse (contains no gaps),
Assuming by even you start counting from 1 (and not 0 like bash does), you can do that with a loop on the indexes:
array=(a b c d e f g h)
for index in "${!array[#]}"; do
(( index % 2 )) && echo "${array[index]}"
done
:
outputs:
b
d
f
h
Assuming you're talking about an indexed rather than associative array and you want the values for the even numbered indices rather than the even number values - loop from zero to array size incrementing the index by 2 on each iteration.
Borrowing #Camunsensei's example:
array=(a b c d e f g h)
for (( index=0; index<${#array[#]}; index+=2 )); do
printf 'array[%d]=%q\n' "$index" "${array[index]}"
done
array[0]=a
array[2]=c
array[4]=e
array[6]=g
If that's not what you need then editing your question to include some sample input, expected output, and what you've tried so far would help a lot.

The "invisible" FS in a comma delimited array entry

Suppose I have:
awk 'BEGIN{
c["1","2","3"]=1
c["12","3"]=2
c["123"]=3 # fleeting...
c["1","23"]=4
c["1" "2" "3"]=5 # will replace c["123"] above...
for (x in c) {
print length(x), x, c[x]
split(x, d, "") # is there something that would split c["12", "3"] into "12, 3"?
# better: some awk / gawkism in one step?
for (i=1; i <= length(x); i++)
printf("|%s|", d[i])
print "\n"
}
}'
Prints:
4 123 4
|1||||2||3|
3 123 5
|1||2||3|
4 123 2
|1||2||||3|
5 123 1
|1||||2||||3|
In each case, the use of the , in forming the array entry produces a visually similar result (123) when printed in the terminal but a distinct hash value. It would appear that there is an 'invisible' separator between the the elements that is lost when printing (i.e., what delimiter makes c["12", "3"] hash differently than c["123"])
What value would I use in split to be able to determine where in the string the comma was placed when the array index was created? i.e., if I created an array entry with c["12","3"] what is the easiest way to print "12","3" vs "123" as a visually distinctly different string (in the terminal) than c["123"]?
(I know that I could do c["12" "," "3"] when creating the array entry. But what makes c["12","3"] hash differently than c["123"] and how to print those so they are seen differently in the terminal...)
c["12","3"] = c["12" SUBSEP "3"]
See SUBSEP in the awk man pages. You can set SUBSEP=FS in the BEGIN section if you have a CSV and want to write c["12","3"] instead of c["12" FS "3"] and have commas printed as the separator in the array indices.

awk to evaluate and count comparisons with sum

I'm trying to figure out the right way to pull this off with awk (not very familiar with awk) but I can't seem to get it.
Basically I have a text file with two columns. I want to sum up the second column and then divide each entry of the second column by the sum and increment a counter if the result is less than 0.25. In order to do this it seems like I have to loop through twice, once to get the sum and once to evaluate each entry with the sum. How can I pull this off with a one-liner?
Example Input:
0 5
1 5
2 10
3 5
Example Output:
3 (the sum is 25 and three of the entries result in a value less than 0.25 when divided by 25)
I got stuck trying to do this in bash and realizing I need to use awk to deal with decimals. I can loop through and get the sum and loop through and do a conditional check on each entry but I don't understand how to do both simultaneously.
Untested:
awk '
{ sum+=$2 ; row[NR]=$2 }
END{ for(i=1;i<=NR;i++) if (row[i]/sum < 0.25) {counter+=1}; print counter }
' file
Using awk
$ awk '{sum+=$2;a[NR]=$2}END{for (i in a) if (a[i]/sum<0.25) count++;print count}' file
Explanation
sum+=$2 get the summary on column 2 and save to sum
a[NR]=$2 record column 2 into array a (NR, the line number, as index)
i in a get index from array a one by one
if (a[i]/sum<0.25) count++ do the calculate and increase count with the condition (<0.25)

Why does awk seem to randomize the array?

If you look at output of this awk test, you see that array in awk seems to be printed at some random pattern. It seems to be in same order for same number of input. Why does it do so?
echo "one two three four five six" | awk '{for (i=1;i<=NF;i++) a[i]=$i} END {for (j in a) print j,a[j]}'
4 four
5 five
6 six
1 one
2 two
3 three
echo "P04637 1A1U 1AIE 1C26 1DT7 1GZH 1H26 1HS5 1JSP 1KZY 1MA3 1OLG 1OLH 1PES 1PET 1SAE 1SAF 1SAK 1SAL 1TSR 1TUP 1UOL 1XQH 1YC5 1YCQ" | awk '{for (i=1;i<=NF;i++) a[i]=$i} END {for (j in a) print j,a[j]}'
17 1SAF
4 1C26
18 1SAK
5 1DT7
19 1SAL
6 1GZH
7 1H26
8 1HS5
9 1JSP
10 1KZY
20 1TSR
11 1MA3
21 1TUP
12 1OLG
22 1UOL
13 1OLH
23 1XQH
14 1PES
1 P04637
24 1YC5
15 1PET
2 1A1U
25 1YCQ
16 1SAE
3 1AIE
Why does it do so, is there rule for this?
From 8. Arrays in awk --> 8.5 Scanning All Elements of an Array in the GNU Awk user's guide when referring to the for (value in array) syntax:
The order in which elements of the array are accessed by this
statement is determined by the internal arrangement of the array
elements within awk and cannot be controlled or changed. This can lead
to problems if new elements are added to array by statements in the
loop body; it is not predictable whether or not the for loop will
reach them. Similarly, changing var inside the loop may produce
strange results. It is best to avoid such things.
So if you want to print the array in the order you store it, then you have to use the classical for loop:
for (j=1; j<=NF; j++) print j,a[j]
Example:
$ awk '{for (i=1;i<=NF;i++) a[i]=$i} END {for (j=1; j<=NF; j++) print j,a[j]}' <<< "P04637 1A1U 1AIE 1C26 1DT7 1GZH 1H26 1HS5 1JSP 1KZY 1MA3 1OLG 1OLH 1PES 1PET 1SAE 1SAF 1SAK 1SAL 1TSR 1TUP 1UOL 1XQH 1YC5 1YCQ"
1 P04637
2 1A1U
3 1AIE
4 1C26
5 1DT7
6 1GZH
7 1H26
8 1HS5
9 1JSP
10 1KZY
11 1MA3
12 1OLG
13 1OLH
14 1PES
15 1PET
16 1SAE
17 1SAF
18 1SAK
19 1SAL
20 1TSR
21 1TUP
22 1UOL
23 1XQH
24 1YC5
25 1YCQ
Awk uses hash tables to implement associative arrays. This is just an inherent property of this particular data structure. The location that a particular element is stored into the array depends on the hash of the value. Other factors to consider is the implementation of the hash table. If it is memory efficient, it will limit the range each key gets stored in using the modulus function or some other method. You also may get clashing hash values for different keys so chaining will occur, again affecting the order depending on which key was inserted first.
The construct (key in array) is perfectly fine when used appropriately to loop over every key but you cannot count on the order and you should not update array whilst in the loop as you may end up process array[key] multiple times by mistake.
There is a good decription of hash tables in the book Think Complexity.
The issue is the operator you use to get the array indices, not the fact that the array is stored in a hash table.
The in operator provides the array indices in a random(-looking) order (which IS by default related to the hash table but that's an implementation choice and can be modified).
A for loop that explicitly provides the array indices in a numerically increasing order also operates on the same hash table that the in operator on but that produces output in a specific order regardless.
It's just 2 different ways of getting the array indices, both of which work on a hash table.
man awk and look up the in operator.
If you want to control the output order using the in operator, you can do so with GNU awk (from release 4.0 on) by populating PROCINFO["sorted_in"]. See http://www.gnu.org/software/gawk/manual/gawk.html#Controlling-Array-Traversal for details.
Some common ways to access array indices:
To print array elements in an order you don't care about:
{a[$1]=$0} END{for (i in a) print i, a[i]}
To print array elements in numeric order of indices if the indices are numeric and contiguous starting at 1:
{a[++i]=$0} END{for (i=1;i in a;i++) print i, a[i]}
To print array elements in numeric order of indices if the indices are numeric but non-contiguous:
{a[$1]=$0; min=($1<min?$1:min); max=($1>max?$1:max)} END{for (i=min;i<=max;i++) if (i in a) print i, a[i]}
To print array elements in the order they were seen in the input:
{a[$1]=$0; b[++max]=$1} END{for (i=1;i <= max;i++) print b[i], a[b[i]]}
To print array elements in a specific order of indices using gawk 4.0+:
BEGIN{PROCINFO["sorted_in"]=whatever} {a[$1]=$0} END{for (i in a) print i, a[i]}
For anything else, write your own code and/or see gawk asort() and asorti().
If you are using gawk or mawk, you can also set an env variable WHINY_USERS, which will sort indices before iterating.
Example:
echo "one two three four five six" | WHINY_USERS=true awk '{for (i=1;i<=NF;i++) a[i]=$i} END {for (j in a) print j,a[j]}'
1 one
2 two
3 three
4 four
5 five
6 six
From mawk's manual:
WHINY_USERS
This is an undocumented gawk feature. It tells mawk to sort array indices before it starts to iterate over the elements of an array.

fpdf cell positioning from array

I have an array named Array with its elements: A, B ,C,D,...,Z
I wanna generated a pdf using FPDF which will shows as below:
Elements in Array: A | B | C
D | E | F
.........
X | Y | Z
The code above only shows elements in a single column. I have no idea how to make it to display as i desire. Please help.
$pdf->Cell('50','0','Elements in Array:',0,0,'L');
$pdf->Cell('50','0',' '.$Array[0],0,0,'L');
$pdf->Ln(5);
for($i=1;$i<=count($Array);$i++)
{
$pdf->Cell('50','0','',0,0,'L');
$pdf->Cell('50','0',' '.$Array[$i],0,0,'L');
$pdf->Ln(5);
}
Here's the basic approach; you can work out the details for yourself.
Instead of $i++, use $i += 3, so that each iteration deals with 3 elements of the array. Then, in each iteration, call Cell 3 times, one for each of the three columns. Draw lines, too if you want. (The subscripts you'll use for the 3 elements are $i, $i + 1, and $i + 2.)
Each row of 3 Cells should be positioned at $y. With each iteration, increment $y by whatever spacing seems to work best.
Since the total number of elements isn't necessarily divisible by 3, you'll have to test $i to avoid referencing a nonexistent element, and then break out of the loop.

Resources