How to output only captured groups with sed


How to output only captured groups with sed



Is there any 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?


I wanna make a UDP comunication between two or more computers

1:

Linux, C++ audio capturing (just microphone) library
The key to receive ting this to job is to tell sed to exclude what you don't want to be output as well as specifying what you did want.. PHP/PAM to change user password?
string='This is a sample 123 text and any  987 numbers' echo "$string" | sed -rn 's/[^[:digit:]]*([[:digit:]]+)[^[:digit:]]+([[:digit:]]+)[^[:digit:]]*/\1 \2/p' 
This says:. Amazon Machine Image(AMI) for PHP5
  • don't default to printing each line (-n)
  • exclude zero or more non-digits
  • include one or more digits
  • exclude one or more non-digits
  • include one or more digits
  • exclude zero or more non-digits
  • print the substitution (p)
If you have GNU grep (it may also job in BSD, including OS X):. PIL's Image.show() brings up *two* different viewers
echo "$string" | grep -Po '\d+' 
or variations such as:. Writing a script in Java? [closed]
echo "$string" | grep -Po '(?<=\D )(\d+)' 
The -P option enables Perl Compatible Regular Expressions. Linux / C++: Get Internet IP Address (not local computer's IP)See man 3 pcrepattern or man 3 pcresyntax.. Embedded linux call using system

2:

Sed has up to nine remembered patterns although you need to use escaped parentheses to remember portions of the regular expression.. See here for examples and more detail.

3:

you must use grep.
grep -Eow "[0-9]+" file 

4:

I believe the pattern given in the question was by way of case only, and the goal was to match any pattern. . If you have a sed with the GNU extension allowing insertion of a newline in the pattern space, one suggestion is:.
> set string = "This is a sample 123 text and any  987 numbers" > > set pattern = "[0-9][0-9]*" > echo $string | sed "s/$pattern/\n&\n/g" | sed -n "/$pattern/p" 123 987 > set pattern = "[a-z][a-z]*" > echo $string | sed "s/$pattern/\n&\n/g" | sed -n "/$pattern/p" his is a sample text and any  numbers 
These examples are with tcsh (yes, I know its the wrong shell) with CYGWIN. (Edit: For bash, remove set, and the spaces around =.).

5:

Try.
sed -n -e "/[0-9]/s/^[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\).*$/\1 \2 \3 \4 \5 \6 \7 \8 \9/p" 
I got this under cygwin:.
$ (echo "asdf"; \    echo "1234"; \    echo "asdf1234adsf1234asdf"; \    echo "1m2m3m4m5m6m7m8m9m0m1m2m3m4m5m6m7m8m9") | \   sed -n -e "/[0-9]/s/^[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\).*$/\1 \2 \3 \4 \5 \6 \7 \8 \9/p"  1234 1234 1234 1 2 3 4 5 6 7 8 9 $ 

6:

Give up and use Perl. Since sed does not cut it, let's just throw the towel and use Perl, at least it is LSB while grep GNU extensions are not :-).
  • Print the entire matching part, no matching groups or lookbehind needed:.
    cat <<EOS | perl -lane 'print m/\d+/g' a1 b2 a34 b56 EOS 
    Output:.
    12 3456 
  • Single match per line, often structured data fields:.
    cat <<EOS | perl -lape 's/.*?a(\d+).*/$1/g' a1 b2 a34 b56 EOS 
    Output:.
    1 34 
    With lookbehind:.
    cat <<EOS | perl -lane 'print m/(?<=a)(\d+)/' a1 b2 a34 b56 EOS 
  • Multiple fields:.
    cat <<EOS | perl -lape 's/.*?a(\d+).*?b(\d+).*/$1 $2/g' a1 c0 b2 c0 a34 c0 b56 c0 EOS 
    Output:.
    1 2 34 56 
  • Multiple matches per line, often unstructured data:.
    cat <<EOS | perl -lape 's/.*?a(\d+)|.*/$1 /g' a1 b2 a34 b56 a78 b90 EOS 
    Output:.
    1  34 78 
    With lookbehind:.
    cat EOS<< | perl -lane 'print m/(?<=a)(\d+)/g' a1 b2 a34 b56 a78 b90 EOS 
    Output:.
    1 3478 

7:

It's not what the OP asked for (capturing groups) although you must extract the numbers using:.
S='This is a sample 123 text and any  987 numbers' echo "$S" | sed 's/ /\n/g' | sed -r '/([0-9]+)/ !d' 
Gives the following:.
123 987 


88 out of 100 based on 63 user ratings 558 reviews