我的文件名类似于“XXAR_CUST_INV_pt_PT_BURST.xml”。我只需要“爆裂”作为结果。
注意:文件名可以有多个“_”(下划线)。所以,我需要最后一个下划线和扩展名“.xml”之前的字符串,其中“BURST”
s="XXAR_CUST_INV_pt_PT_BURST.xml"
BUSRTING='';
source <(sed -r 's/(.*)_([^_]*)[.].*/BUSRTING="\1"/' <<< "${s}")
# Result:
BUSRTING=$(printf '%s' "$BUSRTING" | tr '[a-z]' '[A-Z]')
echo BUSRTING=$BUSRTING"
预期结果是 BURST
s="XXAR_CUST_INV_pt_PT_BURST_US.xml"
BUSRTING='';
source <(sed -r 's/(.*)_([^_]*)[.].*/BUSRTING="\1"/' <<< "${s}")
# Result:
BUSRTING=$(printf '%s' "$BUSRTING" | tr '[a-z]' '[A-Z]')
echo BUSRTING=$BUSRTING"
预期结果是美国
答案1
BURSTING=${s%.xml} # cut off extension
BURSTING=${BURSTING##*_} # cut off anything before the last underscore
typeset -u BURSTING # make uppercase