LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-07-2006, 12:16 PM   #1
ToNNiX
LQ Newbie
 
Registered: Nov 2006
Posts: 6

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

Code:
<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

visibility="never-visible"

to say

visibility="visible"

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?
 
Old 12-07-2006, 12:48 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 12-08-2006, 01:29 PM   #3
ToNNiX
LQ Newbie
 
Registered: Nov 2006
Posts: 6

Original Poster
Rep: Reputation: 0
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 either...is this a problem with sed?
 
Old 12-08-2006, 01:44 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
sed's substitution operator, s/pattern/replacement/options, are executed per line of input. It's possible your XML has line breaks in it?
 
Old 12-08-2006, 02:42 PM   #5
ToNNiX
LQ Newbie
 
Registered: Nov 2006
Posts: 6

Original Poster
Rep: Reputation: 0
nevermind i figured it out...

Code:
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
 
  


Reply



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
Regular Expression msvinaykumar Programming 2 08-14-2006 08:48 AM
Need help with regular expression aecaudel Programming 6 11-04-2005 05:28 AM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 10:08 AM
Regular Expression slizadel Programming 4 07-28-2003 05:16 AM
regular expression gumby Programming 3 07-15-2003 12:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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