1828918 Members
2612 Online
109986 Solutions
New Discussion

Consecutive lines

 
SOLVED
Go to solution
maliaka
Advisor

Consecutive lines

cat file1.txt
E1 001
a a1
b b1
E2 002
a a1
aa a2
b b1
c c1
cc c2
E3 003
a a1
aa a2

I need to get the lines between E2 and E3 using awk or sed.
10 points for the best answer.
8 REPLIES 8
Dennis Handly
Acclaimed Contributor
Solution

Re: Consecutive lines

sed -n -e '/E2/,/E3/p'
This will get the lines including E2 and E3.

If you don't want them, you could use more complex logic in sed that would be hard to understand. (I couldn't get /E2/+1 to work.)

Or just use grep -v:
sed -n -e '/E2/,/E3/p' file | grep -v -e E2 -e E3
maliaka
Advisor

Re: Consecutive lines

Thanks Dennis,

What if the file contains the following:

E2 002
a a1
aa a2
b b1
c c1
cc c2
E2 xxx

Notice, E2 is repeated twice but the numbers next to the E2 are different numbers. Let's assume that we know the first E2 has 002 but we don't know what is the number for the next E2? How do you go about getting the lines between them?

Thanks again
maliaka
Advisor

Re: Consecutive lines

I cut the file in my last post, sorry. It contains these entries. I need to get the lines for E2 002 block only.

E2 xxx
a a1
b b1
E2 002
a a1
aa a2
b b1
c c1
cc c2
E2 xxx
a a1
aa a2
James R. Ferguson
Acclaimed Contributor

Re: Consecutive lines

Hi:

> Let's assume that we know the first E2 has 002 but we don't know what is the number for the next E2? How do you go about getting the lines between them?

Dennis's solution works again. Simply qualify a bit more of the starting pattern:

# sed -n -e '/E2 002/,/E2/p' file

Regards!

...JRF...
Peter Nikitka
Honored Contributor

Re: Consecutive lines

Hi,

my favourite solution follows.
- refine your start/end condition as required
- drop the 'next' statement, when you want to include the lines including the limit condition

awk '/^E2 002/ {out=1; next}
/^E2/ {out=0; next}
out' file1.txt

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Sandman!
Honored Contributor

Re: Consecutive lines

Try the ex(1) construct below:

# ex -s +'/^E2 002$/+1;/^E/-1 p | q!' file1.txt

It prints all lines that fall between the pattern that starts with "E2 002" and the pattern that ends with "E[23]" etc.

~cheers
Hein van den Heuvel
Honored Contributor

Re: Consecutive lines

Pert, I like the solution you list as favourite also for anyting that is slightly more complex than a simple range.

The bare 'out' is a little obscure, but effective.
It is of course a test for the variable out and if true the (empty) statement following it is executed, the default statement being a print of the current line.
Much like: (out!=0) { print $0 }

You can make it slight more tricky, obscure and shorter by carefully arranging the test and sets:

awk '/^E2/ {out=0} out; /^E2 002/ {out=1}' tmp.txt

:-)

Hein.
maliaka
Advisor

Re: Consecutive lines

You guys are awesome!
Thanks