views:

30

answers:

2

I want to match a part of a line for example in the sentence: "The super id=42 or something."

I want to return the number 42.

echo "The super id=42 or something" | grep -o 'id=[0-9]+' | sed 's/id=//'

Would return the correct answer, but is there a more elegant way of solving this, for example by using only one tool?

A: 

For something quick and simple, that's perfectly fine. You could do it all in one command/statement with perl but grep + sed like that should work just fine.

Is this part of a larger toolchain/application or is it just for your own ad hoc needs?

Daniel DiPaolo
It's really just for a script, but I've ran into this sort of situation quite a few times, and I wondered if there is a tool that can do the following: /id=([0-9]+/\1/ without too much extra code.
Enfenion
A: 

The —or rather, a— perl version would be

echo "The super id=42 or something" | perl -ne '/id=(\d+)/ and print "$1\n"'
intuited