KoneLinx
KoneLinx

Reputation: 118

Cut string until specified character. Bash

I'm trying to cut a string until it's first specified characters. in this case, it's first Latin letter.

I tired this. It kind of works but sometimes shifts 1 or more characters.

f=$(echo $f | tail -c $((${#f}+-$(expr index "$f" [azertyuiopqsdfghjklmwxcvbnAZERTYUIOPQSDFGHJKLMWXCVBN])+2)))

take this string as example: ã%82¹ã%83%91ã%83¼ã%82¯ã%83«__original_ver.__-Your_name.mp3

I want to get: original_ver.__-Your_name.mp3 I tend to get this instead: ver.__-Your_name.mp3

Is there a better method? if so, some explanation is always welcome.

Upvotes: 0

Views: 275

Answers (1)

PesaThe
PesaThe

Reputation: 7499

You can use extended globbing:

f=$(shopt -s extglob; LC_ALL=C; echo "${f##+([^[:alpha:]])}")
f=$(shopt -s extglob; LC_ALL=C; echo "${f/#+([^[:alpha:]])}")

or sed:

f=$(LC_ALL=C sed -r 's/^[^[:alpha:]]+//' <<< "$f")

Setting LC_ALL to C is mandatory, otherwise [[:alpha:]] might match wrong characters.

Upvotes: 1

Related Questions