我有一个数据文件
cell (HB)
input
input
input
Z
output
A
input
cell (BP)
input
input
Z1
output
A1
input
我希望输出为
cell (HB)
Z
output
A
input
cell (BP)
Z1
output
A1
input
我想删除cell
行之后出现input
的所有连续行
I tried code
awk '{for (i=1;i<=NF;i++) if (!a[$i]++) print($i,FS)}{print("n")}' file
但是没有期望的改变。
您所展示的样品,请尝试以下。在GNUawk
下编写和测试。
awk '
!/input/{
if(count==1){
print prev
}
count=0
prev=""
}
/input/{
count++
prev=$0
next
}
1
END{
if(count==1){
print prev
}
}
' Input_file
使用gnu-awk
,您可以使用RS
和RT
:
awk -v RS='cell [^n]*n(inputn)+' '{sub(/n.+/, "n", RT); ORS=RT} 1' file
cell (HB)
Z
output
A
input
cell (BP)
Z1
output
A1
input
:
-v RS='cell [^n]*n(inputn)+'
将RS
设置为cell
,后跟空格,直到换行符,然后是包含文本input
的多行。sub(...)
:移除第一个换行符后的所有内容ORS=RT
:设置输出记录分隔符与RT
中包含的文本相同1
:打印ORS
的每个记录
比预期的短,我想知道是否有什么问题:
$ awk '!(f&&/input/){print;f=0}/cell/{f=1}' file
输出:
cell (HB)
Z
output
A
input
cell (BP)
Z1
output
A1
input
这可能适合您(GNU sed):
sed -E ':a;N;s/(cell.*)n.*input/1/;ta;P;D' file
通过设置-E
打开扩展regexp。
打开两行窗口
如果该行包含cell
,然后下一行包含input
,则删除最后一行并重复。
否则,打印/删除第一行并重复。
此通用解决方案将重复行一起删除。
sed -E 'N;/^(.*)n1$/{:a;s/n.*//;$!{N;/^(.*)n1$/ba};D};P;D' file
通过设置-E
打开扩展regexp。
打开两行窗口
如果窗口中的行是重复的,则删除最后一行并继续这样做,直到两行不同,然后删除第一行。
否则,打印/删除第一行并重复。