Question: I have a number of scripts in a directory which take the format of 120100\_0019\_0\_X0434147\_script\_name.sql

I need to select the first 11 numbers only (excluding the underlines)

For the above script the result I need is 12010000190

I can get the result by using the following line but I was hoping someone could explain how I could achieve this result using a shorter code or with `sed` only.

```
echo "$SCRIPT" | cut -c 1-13 | sed 's/_//g'

```

Answer: 
bash only approach:

```
$ script=120100_0019_0_X0434147_script_name.sql

$ script1=${script//_/} # Remove all _
$ script1=${script1:0:11} #Take first 11 characters
$ echo $script1
12010000190

```

OR

```
$ script1=${script//_/} # Remove all _
$ script1=${script1/X*/} # Remove everything starting from X
$ echo $script1
12010000190

```

OR

```
$ script1=${script//_/} # Remove all _
$ script1=${script1/[^0-9]*/} # Remove everything starting from first non-numeric character.
$ echo $script1
12010000190

```
You can do this

```
echo '120100_0019_0_X0434147_script_name.sql' | sed -r 's/_//g; s/(^[0-9]{11}).*/\1/'

```

which first remove the '\_' then select the first 11 digits ignoring the rest of the line.