Re: Bash substrings – just can’t figure it out…

Johnny Rosenberg gurus.knugum at gmail.com
Wed Feb 1 18:29:44 UTC 2012


2012/1/31 Wes James <comptekki at gmail.com>:
> On Tue, Jan 31, 2012 at 11:07 AM, Johnny Rosenberg
> <gurus.knugum at gmail.com> wrote:
>> I have read some about those built in substring tools built into Bash,
>> but still I don't understand them enough to use them.
>
> Or use expr:
>
> File="03. Rock Nuts.flac"
> echo `expr "$File2" : '.*\. \(.*\)\..*'`
>
> -wes
>

That inspired me to try with sed only (in an earlier example I used
awk and sed to obtain the same thing):

echo $File | sed 's/.*\. \(.*\)\..*/\1/'

Thanks! I wonder why I didn't think of that.

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.


Kind regards

Johnny Rosenberg
ジョニー・ローゼンバーグ




More information about the ubuntu-users mailing list