Working on a script where I need to take a string, and remove everything after the last occurence of a certain character. In this case a hyphen.
For example, This-is-a-filename-0001.jpg
should result in This-is-a-filename
Working on a script where I need to take a string, and remove everything after the last occurence of a certain character. In this case a hyphen.
For example, This-is-a-filename-0001.jpg
should result in This-is-a-filename
You can cut strings in bash:
line="This-is-a-filename-0001.jpg"
echo "${line%-*}" # prints: This-is-a-filename
The %-*
operator removes all beginning with the last hyphen.
You're looking for a sed
within your script, something close to what's below.
sed 's!/[^/]*$!/!'
Generally, I would say, please do research before posting a question like yours since it's relatively easy to find the answers