![awk 脚本来识别不正确的单词序列](https://linux22.com/image/101016/awk%20%E8%84%9A%E6%9C%AC%E6%9D%A5%E8%AF%86%E5%88%AB%E4%B8%8D%E6%AD%A3%E7%A1%AE%E7%9A%84%E5%8D%95%E8%AF%8D%E5%BA%8F%E5%88%97.png)
我有一个文件,如下所示:
FHEAD01
THEAD02
TCUST03
TITEM04
TITEM05
IDISC06
TTEND07
TTAIL08
THEAD09
TCUST10
IDISC11....
TITEM12
TTEND13
TTAIL14
THEAD15
TITEM16
IDISC17
TTEND18
TTAIL19
THEAD20
IDISC21.....
TITEM22
TTEND23
TTAIL24
THEAD25
TITEM26
IDISC27
IDISC28
TITEM29
TTEND30
TTAIL31
FTAIL32
我想识别以下序列并打印记录:
1) THEAD,TCUST,IDISC,*
2) THEAD,IDISC,*
预期输出 应打印以下记录
THEAD09
TCUST10
IDISC11
THEAD20
IDISC21
我尝试了以下代码,但没有给出结果:
awk '/THEAD/; /TCUST/ ; /IDISC/ ' test1.dat
答案1
awk '
/THEAD/{
p=$0 #store line with `THEAD`
getline #read next line from file
if(/TCUST/){ #if there is `TCUST` in it
p=p ORS $0 #add it to `p` vaiable
getline #and get next line from file
}
if(/IDISC/) #if there is `IDISC` in line
print p ORS $0 #print `p` variable altogether with line
}
' test1.dat