LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Sed Help (https://www.linuxquestions.org/questions/programming-9/sed-help-911864/)

p1xty 11-04-2011 10:51 AM

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.

grail 11-04-2011 11:03 AM

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

p1xty 11-04-2011 11:16 AM

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.

crts 11-04-2011 11:23 AM

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.

p1xty 11-04-2011 11:30 AM

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.

crts 11-04-2011 11:52 AM

Ok,

in this case:
Code:

sed '/^example/ {:a n; /^example/! ba};d'
will do.

p1xty 11-07-2011 03:25 AM

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.


All times are GMT -5. The time now is 04:12 AM.