2

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

3 Answers 3

5

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 
Sign up to request clarification or add additional context in comments.

8 Comments

Unfortunately I am getting error with this: sed '/line 1/,/line 2/{/line 2/!d}' file in CentOS: d: Event not found.
with this awk code I am also getting error: /line: Event not found.
are you sure you ran it under bash? @Drago it seems that the ! was expanded by your shell, which it shouldn't be.
> echo $0 > /bin/tsh
try to escape the ! then, @Drago. or use bash.
|
2

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 

8 Comments

Good to read that, @Drago :) Note you can accept the answer if your problem is solved.
could you add some remarks how to use the code if in the pattern you have some special characters ... for example /. Thank you again
@Drago for that you need to escape the /. For example awk 'BEGIN{p=1} /.\/line 1/ {p=0} /line 2/ {p=1} p' file would make it it the starting line is ./line 1.
I have this piece of text in the file of interest (llink.inc): aa MATER / 4401001 302 0 0 $# IDMAT MATYP RHO ISINT ISHG ISTRAT IFROZ MATER / 4401005 103 2.753E-6 0 4 after applying: awk 'BEGIN{p=1} /MATER \/ 4401001/ {p=0} /\// {p=1} p' llink1.inc > llink2.inc I don't see any difference between llink1.inc and llink2.inc ... could you help further. I would expect this: aa MATER / 4401005 103 2.753E-6 0 4 Thank you.
@Drago Please update your question with this sample input. In comments it is not possible to see the new lines.
|
0

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 

6 Comments

I am getting here error too: f: Event not found. @Jotne ... do you have an idea about the origin of the problem. Thank you
I do not see why this does not work, works fine in my Ubuntu 12.10. What OS are you on?
I am using CentOS 6.4 with tsh. Thank you
This should work, since its standard commands, try gawk or mawk, it may help.
awk/gawk are bot available in my tcsh but it will issue the same problem, mawk is not available
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.