Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Read text between two lines containing a string

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-08-2013
ysrini ysrini is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 30 May 2014, 11:48 AM EDT
Posts: 67
Thanks: 18
Thanked 1 Time in 1 Post
Read text between two lines containing a string

Hi,
I have text file like the following:

Code:
Start
a
b
121
c
d
End
Start
a
31
e
f
End
Start
p
o
i
k
l
993
End

Now I want to output the Start-End block that contains my search string
So if I search for '121' it should output the first block, search for 'k' then output the last block

Thanks,
-sri
Sponsored Links
    #2  
Old Unix and Linux 02-08-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 31 July 2015, 5:38 PM EDT
Location: Galactic Empire
Posts: 3,420
Thanks: 240
Thanked 1,223 Times in 1,149 Posts

Code:
 
awk -v V="121" ' /Start/ {
        s=x;
        s=$0;
} !/Start/&&!/End/ {
        s=s RS $0;
} $0 == V {
        f = 1;
} /End/ && f == 1 {
        s=s RS $0;
        print s;
        f = 0;
}' file

Sponsored Links
    #3  
Old Unix and Linux 02-08-2013
ysrini ysrini is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 30 May 2014, 11:48 AM EDT
Posts: 67
Thanks: 18
Thanked 1 Time in 1 Post
Got the solution

Code:
awk '/Start/{s=x}{s=s$0"\n"}/121/{p=1}/End/ && p{print s;exit}' <filename>

    #4  
Old Unix and Linux 02-08-2013
Jotne's Unix or Linux Image
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,040
Thanks: 62
Thanked 216 Times in 204 Posts
Converting it each section to rows, simplify handling it.

Code:
awk '{if ($0~"End") {print $0"\n"} else {print $0","}}' ORS="" | awk '/k/'
Start,p,o,i,k,l,993,End

Sponsored Links
    #5  
Old Unix and Linux 02-08-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 31 July 2015, 5:38 PM EDT
Location: Galactic Empire
Posts: 3,420
Thanks: 240
Thanked 1,223 Times in 1,149 Posts
Quote:
Originally Posted by ysrini View Post
Got the solution

Code:
awk '/Start/{s=x}{s=s$0"\n"}/121/{p=1}/End/ && p{print s;exit}' <filename>

Note that this program will not print another section if the search pattern is repeated. But if that is what you want, then it is OK.
Sponsored Links
    #6  
Old Unix and Linux 02-08-2013
ysrini ysrini is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 30 May 2014, 11:48 AM EDT
Posts: 67
Thanks: 18
Thanked 1 Time in 1 Post
Hi bipinajith, the search string will not appear in multiple blocks, thanks for thinking about this.

After I posted my question I found solution based on the following link (credit goes to this):
http://www.unix.com/shell-programmin...o-strings.html
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Read all lines after a string appears in the file. Nagaraja Akkiva Shell Programming and Scripting 4 10-24-2011 07:57 AM
Print lines between two lines after grep for a text string jbruce Shell Programming and Scripting 7 09-29-2010 02:37 PM
Shell script to read lines in a text file and filter user data srimal Shell Programming and Scripting 4 10-23-2009 01:35 AM
Read any lines of text from file versace Shell Programming and Scripting 6 10-20-2009 02:34 AM
Read text file from a specified string to the end bsrajirs UNIX for Advanced & Expert Users 9 01-11-2008 12:05 AM



All times are GMT -4. The time now is 06:38 AM.