Navdeep
Navdeep

Reputation: 55

Substring from end to a letter's index length in unix shell script

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

Upvotes: 1

Views: 2827

Answers (3)

tripleee
tripleee

Reputation: 189678

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.)

Upvotes: 0

janos
janos

Reputation: 124714

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

Upvotes: 2

anubhava
anubhava

Reputation: 785581

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

Upvotes: 2

Related Questions