I want to use 'awk' to extract specific information from a formatted file such that:
Example File:
100 2
A .5 .4
.3 .2 .1
B .9 .8
.7 .6 .65
200 1
A .5 .4
.3 .2 .1
Ideal Output:
100 .9
200 0
Code Thus Far:
awk '{if(NF==2) print $1;}'
Which produces:
100
200
Input
$ cat f
100 2
A .5 .4
.3 .2 .1
B .9 .8
.7 .6 .65
200 1
A .5 .4
.3 .2 .1
Output
$ awk 'NF==2{t=$1; l=(NR+2*$2-1)}NR==l{print t,/^B/?$2:0}' f
100 .9
200 0
Explanation
awk 'NF==2{ # If row has 2 fields
t=$1 # lets save 1st field and print later
l=(NR+2*$2-1) # line to be checked
}
NR==l{ # if current record number is equal to l
# Print t, if starts with B then field 2 to be printed else 0
print t,/^B/?$2:0
}
' f
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With