0

I have a file name like 'XXAR_CUST_INV_pt_PT_BURST.xml'. I need only "BURST" as result.

note: file name can have many "_"'s (Underscores). So, I need string from last underscore and before extension ".xml" which "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"

expected result is 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"

expected result is US

1
BURSTING=${s%.xml}         # cut off extension
BURSTING=${BURSTING##*_}   # cut off anything before the last underscore
typeset -u BURSTING        # make uppercase
  • Thank You. and How do I get the rest of the left string like XXAR_CUST_INV_pt_PT? – Arun GoWdA Jun 18 '18 at 13:54
  • @ArunGoWdA echo "${s%_*}" – ctac_ Jun 18 '18 at 14:22
  • It's typeset -u in ksh88 and ksh93 as well (and zsh, though zsh also has ${(U)BURSTING}) – Stéphane Chazelas Jun 18 '18 at 17:34
  • @StéphaneChazelas hmm, the pdksh manpage insists that -u is “unsigned” in AT&T ksh, though probably only in combination with -i – mirabilos Jun 18 '18 at 21:34
  • Yes, -u is unsigned with -i, and uppercase without, there's no -U there. zsh has -U for unique. The NetBSD sh maintainer was suggesting local -U to create a local variable initially unset to conciliate ash-based shells where local only affects the scope and otherwise keeps the value and attributes of variables and mksh whose unset can't be used on local variables. – Stéphane Chazelas Jun 19 '18 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.