LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 06-16-2010, 02:18 PM   #1
aliencenet
LQ Newbie
 
Registered: Jun 2010
Posts: 1

Rep: Reputation: 0
Extract lines between two patterns containing certain string


Hi all,
I have a file that I need to scan and output data between Number and End containing string 123.
Number 1:
6
7
123
1
End

Number 2:
1
2
End

Number 3:
123
1
2
3
End

The ouput should be:

Number 1:
123
End

Number 3:
123
End

In other words, I need to see block of data when 123 is present between pattern Number and End.

Thank you! any help is appreciated.

I used awk '/Number/{s=x}{s=s$0"\n"}/123/{p=1}/End/ && p{print s;exit}' file > outputfile
but it only return first occurence.
 
Old 06-16-2010, 06:38 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
This seems to work:
Code:
awk -v N=123 'BEGIN{RS=ORS="\n\n" ; OFS="\n"} {if ($0~N){print $1" "$2,N,$NF}}' file
It does leave a blank line at the end though.

(Edited because first version didn't fully solve the request. I also moved the search number into a variable for maximum flexibility.)

Last edited by David the H.; 06-16-2010 at 06:50 PM.
 
1 members found this post helpful.
  


Reply

Tags
awk, programming


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Patterns and string processing in shell scripts LXer Syndicated Linux News 0 12-27-2008 12:30 PM
grepping all the lines between 2 patterns raghu123 Programming 2 11-04-2008 02:24 AM
how to use sed to delete mutiple string patterns from apache access log matyu Programming 3 01-06-2008 12:42 AM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-21-2007 12:14 AM
Help, help! How to find lines in between searched patterns? wujee Linux - Software 1 01-06-2005 03:52 PM


All times are GMT -5. The time now is 02:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration