问题描述
我想删除两个图案之间的线条,但保留第二个图案中的线条.例如对于使用 MATER/4401001 302
和 /
作为模式的文件:
I would like to delete the lines between two patterns but keep the line from the second pattern. For example for the file using MATER / 4401001 302
and /
as a pattern:
$# IDMAT MATYP RHO ISINT ISHG ISTRAT IFROZ
MATER / 4401001 302 0 0 0
$# BLANK QVM IDMPD
0
$# TITLE
NAME PLINK Material
$# SLFACM FSNVL DELTNL STNOR STTAN IFLGC BLANK TLSTIF
0.1 0
$# I3DOF TOLCOR IDRUP
0 1. 0
$---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
$# IDMAT MATYP RHO ISINT ISHG ISTRAT IFROZ
MATER / 4401005 103 2.753E-6 0 4
我想得到:
$# IDMAT MATYP RHO ISINT ISHG ISTRAT IFROZ
MATER / 4401005 103 2.753E-6 0 4
我尝试使用以下代码来做到这一点:
I tried to do it using the following piece of code:
awk 'BEGIN{p=1} /MATER \/ 4401001/ {p=0} /\// {p=1} p' llink1.inc > llink2.inc
但它在我的 CentOS 上不起作用 - 两个文件 llink1.inc
和 llink2.inc
是相同的.
But it doesn't work on my CentOS - the both files llink1.inc
and llink2.inc
are identical.
你能帮忙吗?谢谢
推荐答案
如果 awk
是可能的,这可以是一种方式:
If awk
is possible, this can be a way:
$ awk 'BEGIN{p=1} /line 1/ {p=0} /line 2/ {p=1} p' file
bla bla
line 2
bla bla
bla bla
这是使用p
打印标志的问题,在找到line 1
时取消设置并在line 2
出现时重新设置.
It is a matter of using the p
print flag, unsetting it when line 1
is found and setting it again when line 2
appears.
为了确保我们打印的行是正确的,请说:
To make sure the lines we are printing are the correct ones, say:
$ cat a
1bla bla
line 1
2bla bla
3gov gov
line 2
4bla bla
5bla bla
$ awk 'BEGIN{p=1} /line 1/ {p=0} /line 2/ {p=1} p' a
1bla bla
line 2
4bla bla
5bla bla
鉴于您的新样本输入,这对我有用:
Given your new sample input, this works to me:
$ awk 'BEGIN{p=1} /MATER \/ 4401001/ {p=0; next} /\// {p=1} p' file
$# IDMAT MATYP RHO ISINT ISHG ISTRAT IFROZ
MATER / 4401005 103 2.753E-6 0 4
这篇关于如何删除图案之间的线条但保留带有第二个图案的线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!