Reputation: 101
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
I would like to get:
$# 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
But it doesn't work on my CentOS - the both files llink1.inc
and llink2.inc
are identical.
Could you help. Thank you
Upvotes: 2
Views: 574
Reputation: 290105
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
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
Upvotes: 2
Reputation: 41460
A short awk
awk '/line 1/ {f=1} /line 2/ {f=0} !f' file
bla bla
line 2
bla bla
bla bla
Even shorter, but not robust.
awk '/line/ {f=!f} !f' file
Upvotes: 0
Reputation: 195209
your sed line is close:
sed '/line 1/,/line 2/{/line 2/!d}' file
test
kent$ echo "bla bla
line 1
bla bla
gov gov
line 2
bla bla
bla bla"|sed '/line 1/,/line 2/{/line 2/!d}'
bla bla
line 2
bla bla
bla bla
same idea with awk: (works for the example in question)
awk '/line 1/,/line 2/{if(!/line 2/)next}7' file
same example:
kent$ (master|✚9) echo "bla bla
line 1
bla bla
gov gov
line 2
bla bla
bla bla"|awk '/line 1/,/line 2/{if(!/line 2/)next}7'
bla bla
line 2
bla bla
bla bla
Upvotes: 5