「」と呼ばれるawkチャネルからの標準的なコードがありますFindAllMatches
が、それでも非常に手動で、文字通り、、、、 moreの長いループを作成してからwhile()
、すすぎ、繰り返します。match()
substr()
substr()
一致した部分だけを取得する方法についてのアイデアを探しているが、各行に複数回一致する、またはまったく一致しない複雑な正規表現がある場合は、次のことを試してください。
mawk/mawk2/gawk 'BEGIN { srand(); for(x = 0; x < 128; x++ ) {
alnumstr = sprintf("%s%c", alnumstr , x)
};
gsub(/[^[:alnum:]_=]+|[AEIOUaeiou]+/, "", alnumstr)
# resulting str should be 44-chars long :
# all digits, non-vowels, equal sign =, and underscore _
x = 10; do { nonceFS = nonceFS substr(alnumstr, 1 + int(44*rand()), 1)
} while ( --x ); # you can pick any level of precision you need.
# 10 chars randomly among the set is approx. 54-bits
#
# i prefer this set over all ASCII being these
# just about never require escaping
# feel free to skip the _ or = or r/t/b/v/f/0 if you're concerned.
#
# now you've made a random nonce that can be
# inserted right in the middle of just about ANYTHING
# -- ASCII, Unicode, binary data -- (1) which will always fully
# print out, (2) has extremely low chance of actually
# appearing inside any real word data, and (3) even lower chance
# it accidentally alters the meaning of the underlying data.
# (so intentionally leaving them in there and
# passing it along unix pipes remains quite harmless)
#
# this is essentially the lazy man's approach to making nonces
# that kinda-sorta have some resemblance to base64
# encoded, without having to write such a module (unless u have
# one for awk handy)
regex1 = (..); # build whatever regex you want here
FS = OFS = nonceFS;
} $0 ~ regex1 {
gsub(regex1, nonceFS "&" nonceFS); $0 = $0;
# now you've essentially replicated what gawk patsplit( ) does,
# or gawk's split(..., seps) tracking 2 arrays one for the data
# in between, and one for the seps.
#
# via this method, that can all be done upon the entire $0,
# without any of the hassle (and slow downs) of
# reading from associatively-hashed arrays,
#
# simply print out all your even numbered columns
# those will be the parts of "just the match"
別のを実行する場合OFS = ""; $1 = $1;
、4つの引数split()
またはを必要とする代わりにpatsplit()
、どちらも正規表現のsepsが何であるかを確認するためにgawk固有であるため、$0
のフィールド全体がdata1-sep1-data2-sep2-....パターンになります。 、.....$0
その間、最初にその行を読んだときとまったく同じように見えます。ストレートアップprint
は、読み取り直後に印刷するのと同じバイト単位で行われます。
一度、これで有効なUTF8文字を表す正規表現を使用して極端にテストしました。mawk2が167MBのテキストファイルを処理するのに30秒ほどかかり、すべてが一度に$ 0に読み込まれ、この分割ロジックをクランクすると、NFは約175,000,000になり、各フィールドは1シングルになります。 ASCIIまたはマルチバイトUTF8Unicodeのいずれかの文字。