awk 范围正则表达式没有开始
awk range regex not beginning
我有这样的数据
Data Table ABC
Data
Stop
Data
Data Stop
Data Foo
Data Foo
我正在尝试为 Table ABC 设置一个 awk 范围,直到在同一列中停止。
到目前为止我已经试过了
awk '/Table ABC/,/Stop/' q
Data Table ABC
Data
Stop
但我想得到
Data Table ABC
Data
Stop
Data
Data Stop
awk 范围模式 解决方案:
awk -F"[[:space:]][[:space:]]+" '=="Table ABC", =="Stop"' file
输出:
Data Table ABC
Data
Stop
Data
Data Stop
A range pattern is made of two patterns separated by a comma, in the
form ‘begpat, endpat
’. It is used to match ranges of consecutive
input records. The first pattern, begpat
, controls where the range
begins, while endpat
controls where the pattern ends.
如果数据文件不是表格而是固定长度格式,也许替代解决方案会更好
awk '{m=match([=10=],"Table ABC"); if(m>0) p=m}
p {print; if(p==match([=10=],"Stop")) exit}' file
Data Table ABC
Data
Stop
Data
Data Stop
标记匹配的位置,开始打印,当位置与结束模式匹配时结束。
无论匹配的位置如何,如果您想在结束模式的第二次出现处结束:
awk '/Table ABC/{p=1} p; /Stop/ && ++c==2{exit}' file
计数器c
统计结束模式,第二次退出
我有这样的数据
Data Table ABC
Data
Stop
Data
Data Stop
Data Foo
Data Foo
我正在尝试为 Table ABC 设置一个 awk 范围,直到在同一列中停止。 到目前为止我已经试过了
awk '/Table ABC/,/Stop/' q
Data Table ABC
Data
Stop
但我想得到
Data Table ABC
Data
Stop
Data
Data Stop
awk 范围模式 解决方案:
awk -F"[[:space:]][[:space:]]+" '=="Table ABC", =="Stop"' file
输出:
Data Table ABC
Data
Stop
Data
Data Stop
A range pattern is made of two patterns separated by a comma, in the form ‘
begpat, endpat
’. It is used to match ranges of consecutive input records. The first pattern,begpat
, controls where the range begins, whileendpat
controls where the pattern ends.
如果数据文件不是表格而是固定长度格式,也许替代解决方案会更好
awk '{m=match([=10=],"Table ABC"); if(m>0) p=m}
p {print; if(p==match([=10=],"Stop")) exit}' file
Data Table ABC
Data
Stop
Data
Data Stop
标记匹配的位置,开始打印,当位置与结束模式匹配时结束。
无论匹配的位置如何,如果您想在结束模式的第二次出现处结束:
awk '/Table ABC/{p=1} p; /Stop/ && ++c==2{exit}' file
计数器c
统计结束模式,第二次退出