dwlamb
dwlamb

Reputation: 925

retrieve sub-string starting at mid-point in bash script

Using bash script, is there a simple method to extract a sub-string starting in the middle of a path to filename?

From research I found this http://tldp.org/LDP/abs/html/string-manipulation.html but it seems matching sub-strings starting at a mid-point is not possible.

I have a bash script for cataloguing contents of specific directories and sub-directories that are mirrored on different back-up drives. Using dirname I can assign the path to a variable but I do not want the entire path. Here is a sample of some of the paths I am likely to deal with:

/media/mediaLibrary/movies
/medis/BackUp-01/movies/any_directory
/mnt/BackUp-02/movies/any_directory/any_directory

I want the steps necessary to drop anything in front of movies. The above samples would become:

movies
movies/any_directory
movies/any_directory/any_directory

Upvotes: 2

Views: 71

Answers (1)

anubhava
anubhava

Reputation: 785098

Pure BASH way:

for s in '/media/mediaLibrary/movies' '/medis/BackUp-01/movies/any_directory' '/mnt/BackUp-02/movies/any_directory/any_directory'
do
   echo "${s/*movies/movies}"
done
movies
movies/any_directory
movies/any_directory/any_directory

Upvotes: 1

Related Questions