我需要知道,哪一行是与 awk 的范围模式匹配的行。假设我有以下输入:
some random text
some random text START this is first line I want to match with START
some random text
some random text
some random text
some random text START this line contains another START but I dont want matched this line
some random text
some random text END this line is last of my range
some random text
... input text countinues, more range matches can be found
我想如何匹配:
/START/,/END/ {
if ((index($0,"START"))) {
# do something when range starts
}
#do some other things
}
但后来我意识到,我的索引将在范围中间的第 6 行与 START 匹配。我正在考虑在第一次与索引匹配的 START 后升旗,但是有没有更优雅的方式,比如类似于 NR 的全局变量?
编辑:输入文本继续,可以找到更多范围匹配,如果我提出标志或使用计数,我将不得不在范围结束后重置标志/计数(添加索引($ 0,“END”)),这是我不想要的