Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have many lines like the following being returned from a bash command, with different sourceNodeIds of varying digit lengths:

<NodeAssociation sourceNodeId="33654" [...] sourceNodeEntity="Issue" />

I'd like to pipe it to sed or awk and just return the number nnnn from sourceNodeId="nnnn"

something like:

cat blah | sed 's/.+?sourceNodeId="\(\d+\)".+/\1/'

but this isn't working. I'm on a Mac if that makes any difference (I think the version of sed may be different). I know Perl regexes, but I think sed is expecting a different kind.


share|improve this question
You might consider moving this to stackoverflow, I'm sure we'll be able to help but they might be quicker. – Chopper3 Sep 22 '10 at 16:31
up vote 3 down vote accepted

sed doesn't know about \d and non-greedy matches. You don't need to use cat. This should work:

sed 's/.*sourceNodeId="\([0-9]\+\)".*/\1/' file

Some sed versions are picky about wanting a -e (it will work even if it's not required):

sed -e 's/.*sourceNodeId="\([0-9]\+\)".*/\1/' file

If your sed supports -r you can skip the escaping:

sed -er 's/.*sourceNodeId="([0-9]+)".*/\1/' file
share|improve this answer
thanks! I couldn't get it to work on mac, but it's great on linux. I'm piping from other input, hence the cat. I did have to just use sed -r though. – carillonator Sep 22 '10 at 17:20
On OS X, use -E instead of -r. – Dennis Williamson Jan 10 '11 at 14:05

Also, works:

cat blah | cut -f2 -d\"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.