Reputation: 71
I need to extract a particular word from a string in bash.
I have a String "xxx/yyy/zzz/foo.txt" from this string i need to extract "zzz".
input="xxx/yyy/zzz/foo.txt"
((length = ${#input} - 8))
subString= {$file:0:$length}
name= $subString | rev | cut -d "/" -f1 | rev
echo $name
Here, I got an error like "xxx/yyy/zzz/foo.txt":0:52}: No such file or directory" at line : subString= {$file:0:$length}
Anyone help on this?
Upvotes: 1
Views: 60
Reputation: 13249
If you are dealing with filename, you can use basename
and dirname
commands:
$ input="xxx/yyy/zzz/foo.txt"; echo $(basename $(dirname $input))
zzz
Upvotes: 2