LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Delete text between key words : sed (https://www.linuxquestions.org/questions/programming-9/delete-text-between-key-words-sed-378370/)

shalomajay 10-30-2005 01:09 PM

Delete text between key words : sed
 
I have a file called 'get.txt' as below

*******
cat get.txt

There are five birds three dogs ten chickens and two cats
*******

I need to delete all text from 'three' to 'chickens' including these 2 words like below:

*******
There are five birds and two cats
*******

can i do this using s/keyword1/keyword2/g

Any ideas on how to get this working??

ta,
jo

naf 10-30-2005 01:42 PM

Try:
Code:

sed "s/three.*chickens //g" get.txt

Tinkster 10-30-2005 01:42 PM

Quote:

can i do this using s/keyword1/keyword2/g

Any ideas on how to get this working??
No, your idea won't work, it would replace the 1st keyword
with the second globally.

[edit]heh - beat by twenty seconds :}[/edit]
What you want to do is
s/keyword1.*keyword2//g



Cheers,
Tink

naf 10-30-2005 01:45 PM

gg

shalomajay 10-31-2005 02:20 PM

You guys are great ...thanks

i had to make a small change ...just a space between //

s/keyword1.*keyword2/ /g

thanks guys,

Jo

ajbruscino 05-11-2011 05:57 PM

what if
 
what if I wanted to do the same thing, but remove 's/keyword1.*keyword2//g' but leave keyword1 & keyword2 in place.

using keyword1 and 2 as references btw, cause I could easily use the words before&after each :)

ajbruscino 05-11-2011 06:14 PM

Quote:

Originally Posted by ajbruscino (Post 4353752)
what if I wanted to do the same thing, but remove 's/keyword1.*keyword2//g' but leave keyword1 & keyword2 in place.

using keyword1 and 2 as references btw, cause I could easily use the words before&after each :)

quick example of my file


blahh............................
blahh..................
blahh.........................
blahh.................

I want to remove like s/[a-z].*$//g
that will remove the h at the end, how can I leave that....

ajbruscino 05-11-2011 06:22 PM

Quote:

Originally Posted by ajbruscino (Post 4353757)
quick example of my file


blahh............................
blahh..................
blahh.........................
blahh.................

I want to remove like s/[a-z].*$//g
that will remove the h at the end, how can I leave that....


ok scratch that I figured out how to remove the ...... at the end

but still curious if I have a file that contains


the dog jumps over the stick
the cat crawls under stick


now removing between the.*stick leaving "the stick" behind :)


All times are GMT -5. The time now is 10:15 AM.