LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 10-30-2005, 01:09 PM   #1
shalomajay
LQ Newbie
 
Registered: Jul 2004
Posts: 8

Rep: Reputation: 0
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
 
Old 10-30-2005, 01:42 PM   #2
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
Try:
Code:
sed "s/three.*chickens //g" get.txt
 
Old 10-30-2005, 01:42 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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

Last edited by Tinkster; 10-30-2005 at 01:44 PM.
 
Old 10-30-2005, 01:45 PM   #4
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
gg
 
Old 10-31-2005, 02:20 PM   #5
shalomajay
LQ Newbie
 
Registered: Jul 2004
Posts: 8

Original Poster
Rep: Reputation: 0
You guys are great ...thanks

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

s/keyword1.*keyword2/ /g

thanks guys,

Jo
 
Old 05-11-2011, 05:57 PM   #6
ajbruscino
LQ Newbie
 
Registered: May 2011
Posts: 3

Rep: Reputation: 0
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
 
Old 05-11-2011, 06:14 PM   #7
ajbruscino
LQ Newbie
 
Registered: May 2011
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by ajbruscino View Post
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....
 
Old 05-11-2011, 06:22 PM   #8
ajbruscino
LQ Newbie
 
Registered: May 2011
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by ajbruscino View Post
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
 
  


Reply

Tags
regular expression, replacement, sed



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
Key in Chinese/Japanese words WindowsKiller SUSE / openSUSE 1 11-08-2005 07:50 PM
trying to delete a line with sed deoren Linux - General 2 01-03-2005 09:26 PM
delete words in vi & autologin? hraposo Slackware 6 10-02-2004 10:12 AM
cannot delete certain files with sed ganninu Linux - General 6 12-10-2003 03:31 PM
How can I catch some "KEY WORDS" from a text file without opening it? yuzuohong Linux - General 1 06-14-2002 09:27 PM

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

All times are GMT -5. The time now is 05:07 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