Quantcast
Channel: How to get string between the last two "/" symbols - Super User
Browsing all 5 articles
Browse latest View live

Answer by Vipul for How to get string between the last two "/" symbols

Solved it using awk : echo "/dir0/dir1/dir2/filename.ext" | awk -F'/' '{print $(NF-1)}' The output of above will be: dir2

View Article



Answer by Paulo for How to get string between the last two "/" symbols

Another way with Bash (also two steps). IFS=/ read -a array <<<'/dir0/dir1/dir2/filename.ext' echo ${array[ $(( ${#array[@]}-2 )) ]} dir2

View Article

Answer by AFH for How to get string between the last two "/" symbols

You need to do the parsing in stages:- path=/dir0/dir1/dir2/filename.ext dir=${path%/*} lastdir=${dir##*/} By attempting to combine both parsing operations, you were attempting to use a string instead...

View Article

Answer by Cyrus for How to get string between the last two "/" symbols

With bash and a regex: s="/dir0/dir1/dir2/filename.ext" [[ $s =~ ([^/]*)/[^/]*$ ]] && echo "${BASH_REMATCH[1]}" Output: dir2

View Article

How to get string between the last two "/" symbols

I am using Bash. I have some string of paths, like /dir0/dir1/dir2/filename.ext and I want to extract the string dir2 in bash. You can assume that the word I want to extract is located between the two...

View Article

Browsing all 5 articles
Browse latest View live




Latest Images