Home > OS >  Why can't extract number in a string with sed?
Why can't extract number in a string with sed?

Time:07-25

I can extract port number in the string:

s="2017-04-17 08:16:14 INFO     connecting lh3.googleusercontent.com:443 from 111.111.111.111:26215"
echo $s  | sed  's/\(.*\):\(\d*\)/\2/'
26215

Almost the same knowledge,i want to extract number following "#" in a string:

s="alsa_output.pci-0000_09_00.6.analog-stereo.monitor/#4"
echo $s | sed   's/\#\([0-9]\)/\1/'
alsa_output.pci-0000_09_00.6.analog-stereo.monitor/4
echo $s | sed   's/#\([0-9]\)/\1/'
alsa_output.pci-0000_09_00.6.analog-stereo.monitor/4

Why the output is not the number "4" ?

CodePudding user response:

You're substituting "#4" with "4", but not changing the front part of the string. In your first example, the ".*" is gobbling up the first part. By analogy:

echo $s | sed 's/.*\#\([0-9]\)/\1/'

CodePudding user response:

Your code is only removing the # hash character but retaining everything else as it is not included in the match. You could just remove everything up to and including the hash.

$ sed 's/.*#//' <<< "$s"
4

If back referencing must be used, you can use;

$ sed -E 's/.*#([0-9] )/\1/' <<< "$s"
4

CodePudding user response:

If you want to consider gnu-grep then it is easier with:

s="alsa_output.pci-0000_09_00.6.analog-stereo.monitor/#4"
grep -oP '(?<=#)\d ' <<< "$s"

4

Or using awk:

awk 'match($0, /#[0-9] /) {print substr($0, RSTART 1, RLENGTH-1)}' <<< "$s"

4

CodePudding user response:

echo "alsa_output.pci-0000_09_00.6.analog-stereo.monitor/#4" | 

mawk   NF OFS= FS='^.*#|[^0-9]*$' 
4

If you know only numbers at the tail after # then even simpler :

gawk '$_ = $NF' FS='^. #' 
4
  • Related