conor.ob
conor.ob

Reputation: 97

Regular expression to select first N number of characters except a certain character

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'

Upvotes: 0

Views: 159

Answers (3)

anishsane
anishsane

Reputation: 20980

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

Upvotes: 1

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89584

Without regular expression, you can write:

echo "${SCRIPT::13}" | tr -d _

Upvotes: 0

Diego Torres Milano
Diego Torres Milano

Reputation: 69348

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.

Upvotes: 0

Related Questions