Bash substrings – just can’t figure it out…

Nils Kassube kassube at gmx.net
Wed Feb 1 21:15:02 UTC 2012


Johnny Rosenberg wrote:
> Slightly off topic:
> I am still a bit proud over that I figured out a way to get the album
> artist out of the path to the flac file. The path looks something
> like this:
> Folder="/some/subfolders/and/some/more/subfolders/then/artist/album"
> 
> The artist is always the last but one level of the path, but the
> total number of levels may vary.
> So here is my one-liner, using awk:
> Artist=$(echo "$Folder" | awk -F "/" '{print $((NF-1))}')
> 
> Took me a while to figure out how to use $NF in the print statement.
> Of course I have no doubts that there is a better solution, so if you
> have one, please share. I love to learn! Still a newbie though.

How about this?

Artist=$(basename $(dirname "$Folder"))


Nils




More information about the ubuntu-users mailing list