views:

264

answers:

4

Greetings!

This are well know Bash parameter expansion patterns:

${parameter#word}, ${parameter##word}

and

${parameter%word}, ${parameter%%word}

I need to chop one part from the beginning and anoter part from the trailing of the parameter. Could you advice something for me please?

+1  A: 

I don't know that there's an easy way to do this without resorting to sub-shells, something you probably want to avoid for efficiency. I would just use:

> xx=hello_there
> yy=${xx#he}
> zz=${yy%re}
> echo ${zz}
llo_the

If you're not fussed about efficiency and just want a one-liner:

> zz=$(echo ${xx%re} | sed 's/^he//')
> echo ${zz}
llo_the

Keep in mind that this second method starts sub-shells - it's not something I'd be doing a lot of if your script has to run fast.

paxdiablo
Thank you! I'm currently usit the variation of your first method:DIRNAME="./ABC/abc.txt" DIRNAME=${DIRNAME#./} DIRNAME=${DIRNAME%/*.txt} But I don't understand why this method involves sub-shells...
Andrey Kazak
The first method doesn't involve sub-shells, it's only the second that does that. I'll clarify.
paxdiablo
Is it possible somehow to incorporate two lines of your first method to a one line?
Andrey Kazak
Yes: `yy=${xx#he} ; zz=${yy%re}` :-) If you mean a single _command_, no, I don't believe so but someone with more `bash` knowledge than me may come along later.
paxdiablo
+1  A: 

If you're using Bash version >= 3.2, you can use regular expression matching with a capture group to retrieve the value in one command:

$ path='/xxx/yyy/zzz/ABC/abc.txt'
$ [[ $path =~ ^.*/([^/]*)/.*$ ]]
$ echo ${BASH_REMATCH[1]}
ABC

This would be equivalent to:

$ path='/xxx/yyy/zzz/ABC/abc.txt'
$ path=$(echo "$path" | sed 's|^.*/\([^/]*\)/.*$|\1|p')
$ echo $path
ABC
Dennis Williamson
A: 

This solution uses what Andrey asked for and it does not employ any external tool. Strategy: Use the % parameter expansion to remove the file name, then use the ## to remove all but the last directory:

$ path=/path/to/my/last_dir/filename.txt

$ dir=${path%/*}     

$ echo $dir
/path/to/my/last_dir

$ dir=${dir##*/}

$ echo $dir
last_dir
Hai Vu
A: 

I would highly recommend going with bash arrays as their performance is just over 3x faster than regular expression matching.

$ path='/xxx/yyy/zzz/ABC/abc.txt'
$ IFS='/' arr=( $path )
$ echo ${arr[${#arr[@]}-2]}
ABC

This works by telling bash that each element of the array is separated by a forward slash / via IFS='/'. We access the penultimate element of the array by first determining how many elements are in the array via ${#arr[@]} then subtracting 2 and using that as the index to the array.

SiegeX