LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2011, 10:51 AM   #1
p1xty
LQ Newbie
 
Registered: Nov 2011
Posts: 6

Rep: Reputation: Disabled
Sed Help


Hello,

I need to remove a paragraph of text between 2 words in a text file.


example
Random text Random textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom text

example


I need to take the first occurrence of the word example and all of the text that follows up until the next occurrence of the word example and place it in a separate file. I am unsure of how to do this with sed.
 
Old 11-04-2011, 11:03 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
What have you tried?

Here is a good site to get you started (if you have not got one): http://www.grymoire.com/Unix/Sed.html
 
1 members found this post helpful.
Old 11-04-2011, 11:16 AM   #3
p1xty
LQ Newbie
 
Registered: Nov 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
I tried:

sed -n '/example/,/example/d' my.file

my.file contains:

example
Random text Random textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom text

example


shown in the original post, and some text before and after it. I'm expecting to get rid of what's before and after that text but that doesn't happen. Also, ideally I'd like to get rid of the second occurrence of "example" as well.
 
Old 11-04-2011, 11:23 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

just to be clear:
After processing there will be two files. The original file which has been altered and a new additional file, right?
Code:
$ cat file
no removal
example
Random text Random textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom text

example
no removal
$ cat file
no removal
no removal
$ sed -i '/^example/ {:a N; /\nexample/! ba;w newfile
d}' file
$ cat newfile
example
Random text Random textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom textRandom text

example
[EDIT]
I just see that you modified your requirement with your last post. Post some represantative samples of what the actual data looks like and what it is supposed to look after processing.

Last edited by crts; 11-04-2011 at 11:25 AM.
 
Old 11-04-2011, 11:30 AM   #5
p1xty
LQ Newbie
 
Registered: Nov 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Sorry, perhaps I was unclear. Lets take example file called myFile.txt

myFile.txt contents:

Earlier unimportant info.
Earlier unimportant info.

Keyword
Line 1 of unimportant info.
Line 2 of unimportant info.
Line 3 of unimportant info.
Line 4 of unimportant info.
Line 5 of unimportant info.

Keyword

Later unimportant info.
Later unimportant info.


What I need to do is find this part:
Keyword
Line 1 of unimportant info.
Line 2 of unimportant info.
Line 3 of unimportant info.
Line 4 of unimportant info.
Line 5 of unimportant info.


And output it to a separate file, myFIle2.txt etc.
 
Old 11-04-2011, 11:52 AM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Ok,

in this case:
Code:
sed '/^example/ {:a n; /^example/! ba};d'
will do.
 
1 members found this post helpful.
Old 11-07-2011, 03:25 AM   #7
p1xty
LQ Newbie
 
Registered: Nov 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
At the moment that works and thanks.

sed '/^example/ {:a n; /^example/! ba};d' file1 > file2

Is what I'm using. Is there anyway to change this so that:

Keyword (It takes everything from the first occurrence and stores it in file2)
Line 1 of unimportant info.
Line 2 of unimportant info.
Line 3 of unimportant info.
Line 4 of unimportant info.
Line 5 of unimportant info.

Keyword(It continues to scan through the original source doing the same thing for each occurrence of the word, taking it and the contents after it to a new file until the the Keyword re-appears.)

Later unimportant info.
Later unimportant info.



I appreciate all of your help.
 
  


Reply

Tags
sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed 's/Tb05.5K5.100/Tb229/' alone but doesn't work in sed file w/ other expressions Radha.jg Programming 6 03-03-2011 07:59 AM
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 07:43 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration