How to print only lines in between two strings using awk | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to print only lines in between two strings using awk

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 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 01-08-2008
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 24 October 2014, 9:49 AM EDT
Posts: 2,662
Thanks: 0
Thanked 17 Times in 17 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 01-08-2008
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
 
Join Date: Feb 2006
Last Activity: 6 October 2014, 10:13 AM EDT
Location: Almerķa, Spain
Posts: 777
Thanks: 24
Thanked 110 Times in 105 Posts
In awk:

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

Sponsored Links
    #5  
Old 01-11-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 23 July 2014, 3:17 AM EDT
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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 05:17 PM.