1753773 Members
5144 Online
108799 Solutions
New Discussion юеВ

Regular expression help

 
Jeff_Traigle
Honored Contributor

Regular expression help

Ok, this is my weakest part of scripting things and there's probably some short regexp I could use with sed to accomplish what I need so I'll pick people's brains on here. :)

I have an XML configuration file that has a line with the following format:



I'm trying to grab the "cluster" portion of the string. However, the id and ordered parameters can be reversed randomly so it's not as simple as I'd assumed at first.
--
Jeff Traigle
5 REPLIES 5
Dennis Handly
Acclaimed Contributor

Re: Regular expression help

What do you want to grab? "cluster_pkg"?
You could just ignore the "ordered" part:

$ sed -e 's/group id="\(.*\)" /\1/' file

This should work as long as "true" doesn't have a space after it.
Dennis Handly
Acclaimed Contributor

Re: Regular expression help

Oops that doesn't handle the reverse. I assume you are only getting lines with the two?
$ sed -e 's/ -e 's/ group id="\(.*\)">/\1/' file
Jeff_Traigle
Honored Contributor

Re: Regular expression help

Close, but not quite there... I modified it a little to get really close to what I need.

testfile contains:




With this command:

sed 's/.*id="\(.*_pkg\)".*/\1/' testfile

It returns:

cluster_pkg
cluster_pkg

I don't want the _pkg portion of the string either though.
--
Jeff Traigle
Jeff_Traigle
Honored Contributor

Re: Regular expression help

Got it! Moved the \) to before the _pkg in the sed command. Yippe! :)

Command looks like this:

sed 's/.*id="\(.*\)_pkg".*/\1/' testfile
--
Jeff Traigle
Dennis Handly
Acclaimed Contributor

Re: Regular expression help

>I don't want the _pkg portion of the string either though.

Ah, that makes it easier.
$ sed 's/.*id="\(.*\)_pkg".*/\1/' testfile