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.

How to print only lines in between two strings using awk

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-08-2008
jisha jisha is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 23 May 2011, 9:23 AM EDT
Posts: 163
Thanks: 10
Thanked 0 Times in 0 Posts
How to print only lines in between two strings using awk

Hi,

I want to print only lines in between two strings and not the strings using awk.

Eg:
OUTPUT
top 2
bottom 1
left 0
right 0
page 66
END
I want to print into a new file only
top 2
bottom 1
left 0
right 0
page 66

Thanks in Advance
JS
Sponsored Links
    #2  
Old Unix and Linux 01-08-2008
ranjithpr ranjithpr is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 20 August 2010, 3:34 AM EDT
Posts: 175
Thanks: 0
Thanked 3 Times in 3 Posts
awk /OUTPUT/,/END/ filename|grep -v 'OUTPUT^JEND'

or

awk /OUTPUT/,/END/ filename|grep -v 'OUTPUT
END'
Sponsored Links
    #3  
Old Unix and Linux 01-08-2008
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 28 January 2015, 8:30 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts
there's a standard algo for doing this. turning on/off a flag

Code:
f=0
while read line
do
 case $line in 
  OUTPUT*) f=1; continue ;;
  END* ) f=0
 esac
 if [ "$f" -eq 1 ]; then
    echo $line 
 fi
done < "file"

    #4  
Old Unix and Linux 01-08-2008
Klashxx's Unix or Linux Image
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
 
Join Date: Feb 2006
Last Activity: 22 April 2015, 10:34 AM EDT
Location: Almerķa, Spain
Posts: 779
Thanks: 24
Thanked 112 Times in 106 Posts
In awk:

Code:
awk ' /OUTPUT/ {flag=1;next} /END/{flag=0} flag { print }' file

Sponsored Links
    #5  
Old Unix and Linux 01-11-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 27 November 2014, 1:23 AM EST
Location: Beijing China
Posts: 1,294
Thanks: 0
Thanked 24 Times in 24 Posts
sed

Hi,

I think this one is easy.


Code:
sed -e '1,/OUTPUT/d' -e '/END/,$d' file

The Following User Says Thank You to summer_cherry For This Useful Post:
vertigo23 (03-11-2011)
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Print lines between two strings multiple occurencies (with sed, awk, or grep) theclem35 Shell Programming and Scripting 8 07-25-2012 04:24 AM
Strings from one file which exactly match to the 1st column of other file and then print lines. AshwaniSharma09 Shell Programming and Scripting 4 08-19-2010 08:16 PM
print lines between two strings but for the first occurence. solved Sangal_ak04 Shell Programming and Scripting 0 04-26-2010 05:40 PM
Compare two strings, and print lines containing mismatches kingpeejay Shell Programming and Scripting 1 07-06-2009 11:23 PM
Print all the lines between 2 specified strings digitalrg Shell Programming and Scripting 7 04-17-2009 08:29 AM



All times are GMT -4. The time now is 04:58 PM.