Andrey Kazak
Andrey Kazak

Reputation: 181

Bash: trim a parameter from both ends

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?

Upvotes: 3

Views: 2901

Answers (4)

SiegeX
SiegeX

Reputation: 140347

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.

Upvotes: 0

Hai Vu
Hai Vu

Reputation: 40733

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

Upvotes: 0

Dennis Williamson
Dennis Williamson

Reputation: 360103

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

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881553

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.

Upvotes: 0

Related Questions