LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

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

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


Hi,
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.

example:
"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
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware
Posts: 298

Rep: Reputation: 50
I did the following test.
1. wrote this script
<code>
#!/bin/sh

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
EOF

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

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
olliesa
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.
 
  


Reply


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


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration