Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract substring until first digit

Tags:

grep

bash

shell

sed

I have the following string:

test1234a or test1234 for example and I want to extract only test from that string.

I tried the follwing

echo "Test12h" | sed -e 's/[0-9]\*$//' but is not working. 

Is there any possibility to extract the substring until first digit?

Please let me know what I miss.

Thank you

like image 391
Husdup Bogdan Avatar asked Dec 02 '22 09:12

Husdup Bogdan


1 Answers

The proper tool for extracting substrings matching a regexp from a command's output is grep. Like,

echo "Test12h" | grep -o '^[^[:digit:]]*'

will output Test.

If Test12h is in a variable, you don't even need external utilities; parameter expansions can easily handle that, e.g:

var='Test12h'
echo "${var%%[[:digit:]]*}"
like image 169
oguz ismail Avatar answered Dec 04 '22 04:12

oguz ismail