Is there a way to tell sed to output only captured groups?
For example, given the input:
This is a sample 123 text and some 987 numbers
And pattern:
/([\d]+)/
Could I get only 123 and 987 output in the way formatted by back references?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The key to getting this to work is to tell
sedto exclude what you don’t want to be output as well as specifying what you do want. This technique depends on knowing how many matches you’re looking for. Thegrepcommand below works for an unspecified number of matches.This says:
-r)-n)p) (on one line)In general, in
sedyou capture groups using parentheses and output what you capture using a back reference:will output "bar". If you use
-r(-Efor OS X) for extended regex, you don’t need to escape the parentheses:There can be up to 9 capture groups and their back references. The back references are numbered in the order the groups appear, but they can be used in any order and can be repeated:
outputs "a bar a".
If you have GNU
grep:It may also work in BSD, including OS X:
These commands will match any number of digit sequences. The output will be on multiple lines.
or variations such as:
The
-Poption enables Perl Compatible Regular Expressions. Seeman 3 pcrepatternorman 3 pcresyntax.