- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Consecutive lines
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2007 03:59 PM
03-27-2007 03:59 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2007 05:37 PM
03-27-2007 05:37 PM
SolutionThis 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
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2007 11:55 PM
03-27-2007 11:55 PM
Re: Consecutive lines
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2007 11:59 PM
03-27-2007 11:59 PM
Re: Consecutive lines
E2 xxx
a a1
b b1
E2 002
a a1
aa a2
b b1
c c1
cc c2
E2 xxx
a a1
aa a2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2007 12:09 AM
03-28-2007 12:09 AM
Re: Consecutive lines
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2007 01:04 AM
03-28-2007 01:04 AM
Re: Consecutive lines
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
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2007 03:27 AM
03-28-2007 03:27 AM
Re: Consecutive lines
# 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
- Tags:
- ex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2007 04:00 AM
03-28-2007 04:00 AM
Re: Consecutive lines
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2007 03:07 PM
03-28-2007 03:07 PM
Re: Consecutive lines
Thanks