awk match() multiple matches -
i have following:
echo as:i:0 uq:i:0 zz:z:mus.sup nm:i:0 md:z:50 zz:z:cas.sup co:z:endofline|awk '{match($0,/zz:z[^ ]*/,m); print m[0], m[1]}'
which unfortunately outputs first entry (out of two):
zz:z:mus.sup
it looks me match() function incapable of storing more 1 match array. unless i'm missing here something...?
if indeed case, kindly suggest awk-based 'matching' alternative allow obtain 2 zz:z entries. note, these not located each time @ same column(!) - hence need of using match() function.
the general idea here obtain @ same awk command values appear @ known column positions (e.g. col1, col2), , values (fetched based on unique signature "zz:z") located @ unknown indexed columns.
in addition, following attempt - using gensub() fails output/print 2 zz:z entries, , identify 1 of 2 (and other 1 upon deprecation of reciprocal..)
echo as:i:0 uq:i:0 zz:z:mus.sup nm:i:0 md:z:50 zz:z:cas.sup co:z:endofline|awk '{val= gensub(/.*(zz:z[^ ]*).*/,"\\1 \\2","g",$0);print val}'
the result in case is:
zz:z:cas.sup
but i'd have result:
zz:z:mus.sup zz:z:cas.sup
you calling wrong function, should using split()
not match()
:
$ echo as:i:0 uq:i:0 zz:z:mus.sup nm:i:0 md:z:50 zz:z:cas.sup co:z:endofline| awk '{split($0,t,/zz:z[^ ]*/,m); print m[1], m[2]}' zz:z:mus.sup zz:z:cas.sup
or print number of occurrences in order appeared in input:
$ echo as:i:0 uq:i:0 zz:z:mus.sup nm:i:0 md:z:50 zz:z:cas.sup co:z:endofline| awk '{split($0,t,/zz:z[^ ]*/,m); (i=1; in m; i++) print m[i]}' zz:z:mus.sup zz:z:cas.sup
that uses gnu awk 4th arg split() using gnu awk 3rd arg match().
if had in non-gnu awk it'd be:
$ echo as:i:0 uq:i:0 zz:z:mus.sup nm:i:0 md:z:50 zz:z:cas.sup co:z:endofline| awk '{while(match($0,/zz:z[^ ]*/)) {print substr($0,rstart,rlength); $0=substr($0,rstart+rlength)}}' zz:z:mus.sup zz:z:cas.sup
Comments
Post a Comment