TheWaterProgrammer
TheWaterProgrammer

Reputation: 8229

How to extract a number out of a string preceded by zeroes

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

Answers (7)

Barbaros Özhan
Barbaros Özhan

Reputation: 65218

You can use sed as

echo "SOMETHING00000076XYZ" | sed "s/[a-zA-Z]//g" | sed "s/^0*//"

  • The first step is for removing all letters
  • The second step is for removing leading zeroes

Upvotes: 1

RavinderSingh13
RavinderSingh13

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

Ed Morton
Ed Morton

Reputation: 203284

$ echo 'SOMETHING00000076XYZ' | awk '{sub(/^[^0-9]+/,""); print $0+0}'
76

Upvotes: 1

stack0114106
stack0114106

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

anubhava
anubhava

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

Jan Zhen
Jan Zhen

Reputation: 73

echo 'SOMETHING00000076XYZ' | grep -o '[1-9][0-9]*'

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

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

Related Questions