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. |
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 |
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. |
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 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. |
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. |
Ok,
in this case: Code:
sed '/^example/ {:a n; /^example/! ba};d' |
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. |