LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 08-14-2007, 01:28 PM   #1
MasterC
Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu
Posts: 12,612

Rep: Reputation: 64
Yet more sed, this time with regex!


Ok, so I've started down a lonesome road where I'm trying to learn the language of the locals, but it's really difficult. The biggest difficulty I'm finding is the pace at which the tutorials I've found move, anyway, onto my questions.

I'm learning how to replace text using sed. I've been practicing on both practice files (so I don't completely dork a system) and real files (once I see how the stuff works on the practice files). It's going pretty good, but now I'm venturing into new territory, regex.

I can't find a howto over at http://www.tldp.org on regex, and so far the most helpful thing was the LQ Wiki entry for it. But, as with most of what I've found, it jumps quickly into the lingo that I don't quite follow, yet...

This time it's with Bugzilla. I'm setting up a new Bugzilla setup, and I don't much care for some of the wording. They have a really "neat" way of using text replacement for terms you don't like, and the default includes replacing words like "bug" with whatever you want, in my case I chose "problem". This is all setup already, and all I had to do was go into the variables file and change the word bug to problem, save and close, and BAM! all instances of bug are replaced with problem. Pretty neat! The way they do this is using a sequence like so:
[% terms.bug %] And then anywhere "bug" would normally be my new word (problem) shows up.

Ok, so now I have some words that aren't in that file, so I've setup a variable in the file for Product. Yet none of the pages have the special sequence [% terms.Product %] in them for that term. So I thought I could use sed and regex to weed out and change my needs easily.

All of that for this:

How can I do this? (I'd also love a crawling tutorial that crawls with me through learning this if anyone has one).

So far, I did this:
Code:
find . -type f -true | xargs -n1 sed -i 's/product/[% terms.product %]/g'
And it worked fairly good. It made a couple of errors that I had to manually fix, like placing that inside another variable [% some-other_variable [% terms.product %] modifier %] I'd have to go in and hard code my replacment (location is my replacement in the variable file).

So, any ideas on how I can avoid replacing the terms inside an existing variable?

If any of that doesn't make sense, please respond and I'll see if I can clarify a bit.

Thanks!

-Chad

Later on that hour:
It just occurred to me that I don't have to use Bugzilla's variable replacement to replace my text, I can just do it with sed and not have to worry about changing it again...

I'm still interested in a response to my above problem even though I won't be using it in this situation.

Thanks!

Last edited by MasterC; 08-14-2007 at 01:37 PM. Reason: Amazing realization
 
Old 08-14-2007, 02:15 PM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
It seems you want to replace " product " with " [% terms.product %] ". This will eliminate the instance you mentioned as well as things like "production". To do this, put the spaces in the regex expression:

xargs -n1 sed -i 's/ product / [% term.product %] /g'

I'm not sure, you may need to escape the spaces.
 
Old 08-16-2007, 12:12 PM   #3
MasterC
Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu
Posts: 12,612

Original Poster
Rep: Reputation: 64
Thank you for the very fast response! Indeed, I do want to eliminate changing things like that, but I also want to eliminate changing [% product %] since this is already a defined variable. I think that there isn't really an "easy" way to do it. I need to set too many possibilities, such as "anywhere in this may exist a [% whatever %] variable, IF product is inside that variable, don't change it.

That and there are other types of documents in the template directory such as java scripts that have product.something as one of their variables.

I've slowly and dreadfully just been going into each file with vim, running a search for roduct (to get both P and p, suffix s or without) and changing it manually where applicable. I'm about 1/4 done, but I'm only spending about 20 minutes a day (or just whenever I get the urge) to work on it, so overall it's not that much work, just seems... I guess kind of "weak" to have to do it this way. And once I started I realized there are other words that I am going to need to change, so I'm still thinking of other ways to do this, and I think there might be a good combo of a regex string and a bit of IF/OR/AND statements in a small script.

If anyone has any further thoughts or ideas on how I might go about this, feel free to respond.

Thanks!

-Chad
 
Old 08-16-2007, 02:22 PM   #4
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Perl regular expressions might do the trick:
Code:
ada@barnabas:~> echo 'whatever [% whatever %] whatever and whatever [% another whatever here %]' |
> perl -pe 's/whatever(?![^[]*?%\])/lala/g'
lala [% whatever %] lala and lala [% another whatever here %]
This matches "whatever" only if it's not followed by a "%]" that is not preceded by "[". Eerie, isn't it?
 
Old 08-16-2007, 03:45 PM   #5
MasterC
Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu
Posts: 12,612

Original Poster
Rep: Reputation: 64
Excellent, thanks I'll give it a whirl!

-Chad
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed regex question ShaqDiesel Programming 1 02-09-2007 07:24 PM
sed RegEx problems InJesus Programming 6 01-12-2007 11:48 AM
sed / regex question whysyn Linux - General 3 06-28-2005 02:11 PM
regex problem with sed ta0kira Programming 7 06-20-2005 12:33 AM
Help with Sed and regex cmfarley19 Programming 6 11-18-2004 01:09 PM


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