-   Linux - Newbie (
-   -   Regular Expression Hell (

ToNNiX 12-07-2006 12:16 PM

Regular Expression Hell
I can't seem to quite figure this out...

I need to use grep and sed to alter the following string in a specific xml file


<column-def-properties db-column-name="ITEM_ID" ...BUNCH OF STUFF HERE... visibility="never-visible" ...MORE JUNK HERE...>
I need to just change the part that says


to say


The one key to this is that there are other XML elements that also have this atribute, so I can not make this a global change.

Currently I have the command

grep -lr "<column-def-properties db-column-name=\"ITEM_ID\"*$visibility" test.xml | xargs sed -i -e 's/visibility=\"never-visible\"/visibility=\"visible\"/'

which is replacing ALL rather than just the one occurance I want...

Any ideas?

matthewg42 12-07-2006 12:48 PM

If you want to do robust parsing of the file, you should probably use an XML parser. using perl or python is probably a better option.

ToNNiX 12-08-2006 01:29 PM

unfortunately i don't have that option...

from what I read in the man pages for sed, it's only supposed to replace the first occurance of the regular expression unless i use the 'g' flag, which I am not doing in this case. I also tried using the NUMBER flag (which is only supposed to replace the NUMBERth occurance) and that did not work as documented this a problem with sed?

matthewg42 12-08-2006 01:44 PM

sed's substitution operator, s/pattern/replacement/options, are executed per line of input. It's possible your XML has line breaks in it?

ToNNiX 12-08-2006 02:42 PM

nevermind i figured it out...


sed -e 's/name=\"ITEM_ID\"\(.*\)visibility=\"never-visible\"/name=\"ITEM_ID\"\1visibility=\"visible\"/' text.xml
the \(.*\) preserves the junk in between ITEM_ID and visibility attributes, and the \1 is like a variable reference used when replacing the regular expression, which inserts the junk back where i need it to be

All times are GMT -5. The time now is 07:46 PM.