1

I have string say,

a="abc_def ghi__333_31122013_Monthly.pdf"

I need the substract monthly and 31122013 from the above string in shell script. Basically doing substring from last till the first index of '_'.

Any help or suggestion is welcome.

Navdeep

Navdeep
  • 55
  • 1
  • 2
  • 6

3 Answers3

2

Using awk:

a="abc_def ghi__333_31122013_Monthly.pdf"
awk -F '[_.]' '{print $(NF-2), $(NF-1)}' <<< "$a"
31122013 Monthly

Using IFS in BASH:

IFS=[_.] && arr=($a)
l=${#arr[@]}
echo ${arr[$l-3]}
31122013
echo ${arr[$l-2]}
Monthly
anubhava
  • 761,203
  • 64
  • 569
  • 643
2

If you want to remove _31122013_Monthly, where 31122013 is variable, you could use a substitution like this:

$ a="abc_def ghi__333_31122013_Monthly.pdf"
$ echo ${a/_????????_Monthly}
abc_def ghi__333.pdf

If on the other hand you really want this:

Basically doing substring from last till the first index of '_'.

then, you could do this:

$ echo ${a/_*_}
abcMonthly.pdf

Or if you want to cut off until the _ right before 31122013_Monthly.pdf then you can:

$ echo ${a#*__*_}
31122013_Monthly.pdf
janos
  • 120,954
  • 29
  • 226
  • 236
0

If you actually want to remove the last sequence of eight digits between underscores and the following alphabetic token up until just before the full stop, try something like

ext=${a##*.}
echo ${a%_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_*}.$ext

(This isn't entirely precise; it will replace the tail from the eight digits with underscores with nothing, then tack the extension back on.)

tripleee
  • 175,061
  • 34
  • 275
  • 318