Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 10-14-2009, 04:26 AM   #1
LQ Newbie
Registered: Oct 2009
Posts: 2

Rep: Reputation: 0
Sed and regex: how to replace up to a certain string

I'm trying to use sed to match a particular string and everything up until some other string. For example, I want to replace "foo" and everything up until "XX" with "bar". However, if "XX" is not in the string, then it should replace everything up until the end of the line.

"foo blah blah XX more text" should become "bar XX more text"
"foo blah blah more text" should become "bar"

Here is what I have so far:

sed 's/foo\(.\(\?!XX\)\)*/bar/g' < input.txt

Thanks for your help!
Old 10-14-2009, 05:02 AM   #2
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 447

Rep: Reputation: 99
I did the following test.
1. wrote this script

cat > input.txt <<EOF
foo blah blah XX more text
foo blah blah more text
blah blah more text
XX foo blah blah more text

cat input.txt
echo '-----------------'
sed 's?XX??g' input.txt | \
sed 's?^[^]*$?&?' |
sed 's?^\([^]*\)\(.\)?bar XX\2?g' |
sed 's?^[^]*$?bar?'
2. then I ran the script and got the output below
$ ./
foo blah blah XX more text
foo blah blah more text
blah blah more text
XX foo blah blah more text
bar XX more text
bar XX foo blah blah more text

Is that what you're looking for?

The first seder replaces XX by the degree character that
shouldn't be present in your input file.
Then this degree character is placed at the end of all the lines that
don't have one already
The 3rd seder replaces the '... degree' by 'bar XX' in the lines where
the degree character is followed by at least one character
The 4th seder replace all the lines that have only one degree
character at the end by 'bar'
Old 10-21-2009, 02:49 AM   #3
LQ Newbie
Registered: Oct 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for the help. That will work, but it is a little cumbersome... I thought there would be a way to do this using negative lookahead in the sed regular expression.


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash replace all matches of regex substring in string nickleus Linux - General 3 04-30-2011 11:08 AM
SED replace string by occurrence uttam_h Programming 5 03-05-2008 10:02 PM
sed replace string octeto Programming 4 06-06-2007 02:09 AM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:56 PM.

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