0

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.

Edited by dwlamb: fixed syntax quoting

4
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by cheochi
0

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

1

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.