2

I want to extract substring till the point the last numeric ends.

for example:

In the string "abcd123z" , I want the output to be "abcd123"

In the string "abcdef123gh01yz" , I want the output to be "abcdef123gh01"

In the string "abcd123" , I want the output to be "abcd123"

How to do this in the unix shell?

0

3 Answers 3

2

Try this sed command,

sed 's/^\(.*[0-9]\).*$/\1/g' file

Example:

$ echo 'abcdef123gh01yz' | sed 's/^\(.*[0-9]\).*$/\1/g'
abcdef123gh01
Sign up to request clarification or add additional context in comments.

Comments

1

You can do this in BASH regex:

str='abcdef123gh01yz'
[[ "$str" =~ ^(.*[[:digit:]]) ]] && echo "${BASH_REMATCH[1]}"
abcdef123gh01

2 Comments

Yes I clearly mentioned BASH because of that since OP tagged the question with BASH
He tagged bash and ksh
1
tmp="${str##*[0-9]}"     # cut off all up to last digit, keep intermediate
echo "${str%$tmp}"        #  remove intermediate from end of string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.