Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-08-2008
Registered User
 
Join Date: Jan 2008
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
Registered User
 
Join Date: Nov 2007
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
Registered User
 
Join Date: Sep 2006
Posts: 2,651
Thanks: 0
Thanked 15 Times in 15 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
Location: Almerķa, Spain
Posts: 696
Thanks: 14
Thanked 84 Times in 80 Posts
In awk:

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

Sponsored Links
    #5  
Old 01-11-2008
Registered User
 
Join Date: Jun 2007
Location: Beijing China
Posts: 1,277
Thanks: 0
Thanked 19 Times in 19 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 09:42 AM.