A script I am writing on works on the DVD present in the drive at time of script execution. I want to extract merely the label of whatever appears after '/media/';

./script.sh /media/DVDRW-43/

An obvious solution would be to get the substring by

path='echo ${file:7:9}' #renders DVDRW-43

but I would rather a substring extraction based on capturing from the second occurence of '/' to third occurence of '/'. I can't get my head around regex or the substring commands to do this.

Recommended Answers

All 4 Replies

Cut might be what you're looking for. Below will give you the text between the 2nd and 3rd "/".
cut -d "/" -f3

Cut works well for that! Or awk:

echo $file | awk -F/ '{print $3}'

You could also do something with sed...

echo $file | sed -n 's%/media/\([^/]*\)/.*%\1%p'

HTH!
-G

Thanks all. These solutions work

Thanks for Sharing....Nice information

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.