FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 01-31-2012, 05:07 PM
Johnny Rosenberg
 
Default Bash substrings – just can’t figure it out…

I have read some about those built in substring tools built into Bash,
but still I don't understand them enough to use them.

Here's what I found at http://tldp.org/LDP/abs/html/string-manipulation.html:
”${string#substring}

Deletes shortest match of $substring from front of $string.
${string##substring}

Deletes longest match of $substring from front of $string.”

I have an example here:
File="03. Rock Nuts.flac"
echo "${File##N*s}"

The result should be ”03. Rock .flac”, shouldn't it? Obviously not,
because the result is ”03. Rock Nuts.flac”, which is exactly the
original string.
So what am I missing?

What I originally really wanted to achieve was something quite
different: The opposite of the above; cutting out everything BUT
something. In this case I wanted to cut everything between ”. ” and
”.”, so that only ”Rock Nuts” is left in the example above. I know I
can do that very easily with awk, but how can I do it with the Bash
built in stuff?



Kind regards

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

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 01-31-2012, 05:52 PM
Johnny Rosenberg
 
Default Bash substrings – just can’t figure it out…

2012/1/31 Cybe R. Wizard <cybe_r_wizard@earthlink.net>:
> On Tue, 31 Jan 2012 12:31:06 -0600
> "Cybe R. Wizard" <cybe_r_wizard@earthlink.net> wrote:
>
>> On Tue, 31 Jan 2012 19:07:20 +0100
>> Johnny Rosenberg <gurus.knugum@gmail.com> wrote:
>>
>> > I have an example here:
>> > File="03. Rock Nuts.flac"
>> > echo "${File##N*s}"
>> >
>> > The result should be ”03. Rock .flac”, shouldn't it? Obviously not,
>> > because the result is ”03. Rock Nuts.flac”, which is exactly the
>> > original string.
>> > So what am I missing?
>>
>> Knowing little about BASH, I'm nevertheless immediately drawn to the
>> fact that your example is missing the space you wish in our output.
>>
>> Cybe
>
> Just noticed that you're trying to pull out, "Nuts."
> Sorry for the noise.

No, there were no noise. I was trying to delete Nuts in my example,
but the following question was about pulling out ”Rock Nuts” in the
same example. Sorry for being confusing.

According to http://tldp.org/LDP/abs/html/string-manipulation.html ##,
#, %% and % is for substring removal and // is for substring
replacement. Isn't that correct?
If it is, shouldn't echo "${File##N*s}" pull out ”Nuts” from my
example? Why not?

”stringZ=abcABC123ABCabc
# |----| shortest
# |----------| longest

echo ${stringZ#a*C} # 123ABCabc
# Strip out shortest match between 'a' and 'C'.

echo ${stringZ##a*C} # abc
# Strip out longest match between 'a' and 'C'.”


So shouldn't, in my example, echo ${File##N*s} ”strip out” longest
match between 'N' and 's'?
What's the difference between my example and theirs, except that mine
doesn't work?


Kind regards

Johnny Rosenberg
ジョニー・*ーゼンバーグ
>
> Cybe R. Wizard
> --

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 01-31-2012, 05:55 PM
Johnny Rosenberg
 
Default Bash substrings – just can’t figure it out…

2012/1/31 Jonathan Hudson <jh+ubuntu@daria.co.uk>:
> On Tue, 31 Jan 2012 12:31:06 -0600, Cybe R. Wizard wrote:
>
>>On Tue, 31 Jan 2012 19:07:20 +0100
>>Johnny Rosenberg <gurus.knugum@gmail.com> wrote:
>>
>>> I have an example here:
>>> File="03. Rock Nuts.flac"
>>> echo "${File##N*s}"
>>>
>>> The result should be ”03. Rock .flac”, shouldn't it? Obviously not,
>>> because the result is ”03. Rock Nuts.flac”, which is exactly the
>>> original string.
>>> So what am I missing?
>
> That it's a string substitution, not a glob or wildcard match.

What's a ”glob”? English is not my native language, sorry. I try the
best I can though.

> Try
>
> $ echo ${File//N*s}

That worked, but somehow I still don't understand exactly why my
example didn't… *confused*


Kind regards

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

>
>
>
> --
> ubuntu-users mailing list
> ubuntu-users@lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 01-31-2012, 06:07 PM
Johnny Rosenberg
 
Default Bash substrings – just can’t figure it out…

If I just give up and use awk and sed, this solves my problem:
echo "$File" | awk -F . '{print $2}' | sed 's/^ //'

But still, is this possible with the Bash built in substring features?


Kind regards

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

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 01-31-2012, 06:40 PM
Johnny Rosenberg
 
Default Bash substrings – just can’t figure it out…

2012/1/31 Jonathan Hudson <jh+ubuntu@daria.co.uk>:
> On Tue, 31 Jan 2012 19:55:24 +0100, Johnny Rosenberg wrote:
>
>>2012/1/31 Jonathan Hudson <jh+ubuntu@daria.co.uk>:
>>> On Tue, 31 Jan 2012 12:31:06 -0600, Cybe R. Wizard wrote:
>>>
>>>>On Tue, 31 Jan 2012 19:07:20 +0100
>>>>Johnny Rosenberg <gurus.knugum@gmail.com> wrote:
>>>>
>>>>> I have an example here:
>>>>> File="03. Rock Nuts.flac"
>>>>> echo "${File##N*s}"
>>>>>
>>>>> The result should be ”03. Rock .flac”, shouldn't it? Obviously not,
>>>>> because the result is ”03. Rock Nuts.flac”, which is exactly the
>>>>> original string.
>>>>> So what am I missing?
>>>
>>> That it's a string substitution, not a glob or wildcard match.
>>
>>What's a ”glob”? English is not my native language, sorry. I try the
>>best I can though.
>>
>>> Try
>>>
>>> $ echo ${File//N*s}
>>
>>That worked, but somehow I still don't understand exactly why my
>>example didn't… *confused*
>>
>>
> First ## is a glob (pattern match), apologies. The reason why your ##
> doesn't work is because (a) it is the prefix match, not a suffix match
> and (b) it tries to match the whole remainder of the string (which ends
> in 'c', not 's), whilst // is a replacement.
>
> $ echo ${File%%N*c}
>
> illustrates the suffix (end bit) match.
>
> and
>
> $ echo ${File##03. }
>
> shows the prefix match.
>
> -jh

Aaaah, so I can only match from any character to the last character or
from the first to the last character? Ok, that's maybe useful in some
situations. So in my case, I would need two lines to obtain what I'm
trying to do:
Title=${File##03. }
Title=${Title%%.*c}

$ echo $Title
Rock Nuts

Well, at least it works, but a one-liner would be nice…

I found that the ”c” is very unnecessary. This seems to work the same way:
Title=${File#* }
Title=${Title%.*}


Kind regards

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

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 01-31-2012, 08:17 PM
Wes James
 
Default Bash substrings – just can’t figure it out…

On Tue, Jan 31, 2012 at 11:07 AM, Johnny Rosenberg
<gurus.knugum@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

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-01-2012, 05:29 PM
Johnny Rosenberg
 
Default Bash substrings – just can’t figure it out…

2012/1/31 Wes James <comptekki@gmail.com>:
> On Tue, Jan 31, 2012 at 11:07 AM, Johnny Rosenberg
> <gurus.knugum@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
ジョニー・*ーゼンバーグ

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-01-2012, 09:04 PM
Johnny Rosenberg
 
Default Bash substrings – just can’t figure it out…

2012/2/1 Nils Kassube <kassube@gmx.net>:
> 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"))

Oh… yes, of course! Didn't think of that…


Kind regards

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

>
>
> Nils
>
> --
> ubuntu-users mailing list
> ubuntu-users@lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 

Thread Tools




All times are GMT. The time now is 09:54 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org