Reputation: 8229
I got a string that looks like this SOMETHING00000076XYZ
How can I extract the number 76
out of the string using a shell script? Note that 76
is preceded by zeroes and followed by letters.
Upvotes: 0
Views: 100
Reputation: 65218
You can use sed
as
echo "SOMETHING00000076XYZ" | sed "s/[a-zA-Z]//g" | sed "s/^0*//"
Upvotes: 1
Reputation: 133458
1st solution: If you are ok with awk
could you please try following.
echo "SOMETHING00000076XYZ" | awk 'match($0,/0+[0-9]+/){val=substr($0,RSTART,RLENGTH);sub(/0+/,"",val);print val;val=""}'
In case you want to save this into a variable use following.
variable="$(echo "SOMETHING00000076XYZ" | awk '{sub(/.*[^1-9]0+/,"");sub(/[a-zA-Z]+/,"")} 1')"
2nd solution: Adding 1 more awk
solution here(keeping your sample in mind).
echo "SOMETHING00000076XYZ" | awk '{sub(/.*[^1-9]0+/,"");sub(/[a-zA-Z]+/,"")} 1'
Upvotes: 1
Reputation: 203284
$ echo 'SOMETHING00000076XYZ' | awk '{sub(/^[^0-9]+/,""); print $0+0}'
76
Upvotes: 1
Reputation: 8711
You can try Perl as well
$ echo "SOMETHING00000076XYZ" | perl -ne ' /\D+0+(\d+)/ and print $1 '
76
$ a=$(echo "SOMETHING00000076XYZ" | perl -ne ' /\D+0+(\d+)/ and print $1 ')
$ echo $a
76
$
Upvotes: 1
Reputation: 785038
Using gnu grep
:
grep -oP '0+\K\d+' <<< 'SOMETHING00000076XYZ'
76
\K
resets any matched information.
Here is another variant of awk
:
awk -F '0+' 'match($2, /^[0-9]+/){print substr($2, 1, RLENGTH)}' <<< 'SOMETHING00000076XYZ'
76
Upvotes: 1
Reputation: 521053
Here is a sed
option:
echo "SOMETHING00000076XYZ" | sed -r 's/[^0-9]*0*([0-9]+).*/\1/g';
76
Here is an explanation of the regex pattern used:
[^0-9]* match zero or more non digits
0* match zero or more 0's
([0-9]+) match AND capture any quantity of non zero digits
.* match the remainder of the string
Then, we just replace with \1
, which is the first (and only) capture group.
Upvotes: 1