The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Filter some text Danish Shakil Shell Programming and Scripting 3 07-31-2008 05:58 AM
Remove duplicate text dejavu88 Shell Programming and Scripting 7 06-08-2008 05:37 PM
Filter data from text file b_sri Windows & DOS: Issues & Discussions 18 04-16-2008 11:41 AM
Deleting text block in file andre123 Shell Programming and Scripting 1 02-27-2007 01:17 PM
using sed(?) to delete a block of text toast Shell Programming and Scripting 3 10-23-2005 02:30 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-07-2008
dkumar91 dkumar91 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 2
Filter duplicate block of text using SED

Hi,

I would like to print a block of text between 2 regular expression using Sed,
This can be achieved by using the command as shown below, however my problem is the same block of text is repeated twice. I would like to eliminate the duplicate block of text.

For Example

If my file test.txt contains following data.
**********************************************
start
test for the block
test for the block
test for the block
End
Blah Blah
Blah Blah
Blah Blah
start
test for the block
test for the block
test for the block
end

*******************
Now if i use sed command to print the text between regular expressions
"start" and "end"

sed -n '/start/,/end/p' text.txt >> ouput.txt

I get the block of text twice in output.txt file as shown below
********************************************************************
start
test for the block
test for the block
test for the block
end
start
test for the block
test for the block
test for the block
end
****************

Please help on how do I filter duplicate printing.

Thanks in Advance
Deepak
  #2 (permalink)  
Old 08-07-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Make sed quit when it encounters the end.


Code:
sed -n '/start/,/end/p;/end/q' text.txt >> output.txt

  #3 (permalink)  
Old 08-07-2008
dkumar91 dkumar91 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 2
Hi,

Thanks for your help and time on this, It works great.

Deepak.
  #4 (permalink)  
Old 10-22-2008
ankimo ankimo is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 23
I'd like to piggy-back onto this post and ask, how do I read everything in between "start" and "end" so that "start" and "end" are not included in the extraction?

I recognize that I can use different words, but I want to keep "start" and "end" in my file. The reason is that I created a help page for a script I wrote. When option -h is used, it grabs the text from the file. It would be handy to have all my help pages begin with "start" and end with "end". But I don't want those words to display on the screen.

Thanks.
  #5 (permalink)  
Old 10-23-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,089

Code:
sed -n '/start/,/end/p
 /end/q
 ' filename | sed '/^start$/d
/^end$/d'

  #6 (permalink)  
Old 10-23-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Or another idea:


Code:
awk '/^start$/ { while (getline && $0 !~ /^end$/) print }' inputfile

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0