[Techtalk] substring up to the first non-match character

Wim De Smet kromagg at gmail.com
Mon Feb 25 08:48:39 UTC 2013


Hi,

On Sun, Feb 24, 2013 at 5:40 AM, Miriam English <mim at miriam-english.org>wrote:

> I have two strings and I want to find the matching string plus the first
> non-matching character, starting from the first character.
>
> I've written a simple bash loop that works its way forward through the
> strings ($str1 and $str2), dropping out when it encounters the first
> character that doesn't match.
>
>         idx="0"
>         while [ "${str1:$idx:1}" = "${str2:$idx:1}" ]
>         do
>             ((idx += 1))
>         done
>         ((idx += 1))
>         str3="${str2:0:$idx}"
>
> If $str1 is "abcdyz" and $str2 is "abcdmnopq" then $str3 is "abcdm"
> If $str1 is "abc" and $str2 is "wxyz" then $str3 is "w"
>
> It works well enough, however it bugs me that it's a bit clumsy and I'm
> haunted by the vague recollection that there is a single command that will
> do the job. Does anybody have any idea of what that command may be?
>

I was thinking about a few ways to do this, but no simple solution comes to
mind. Maybe a sort of split them up into one character per line (with sed),
interleave them with paste, then find the first line where there is not a
double (with uniq somehow). But then paste normally works on files, and
using temporary files for such a simple task... Maybe someone can come up
with a better solution than me based on that. :)


>
> As explanation of its purpose, it is part of a little script I wrote that
> lets me use an empty placeholder file to mark my position in a sequence of
> audio files I'm listening to. I right-click the placeholder after I've
> listened to one of the files and choose "increment" (the name of my script)
> and it renames the placeholder so that it is repositioned in the
> file-listing after the file that I just finished listening to, and before
> the next one. So I can come back weeks or months later and I know where I'm
> up to. Cool, huh?
>

Could try to find the line number of your current file in the listing (ls |
grep), get the next file (nr + 1) and simply edit that name somehow so your
placeholder is in the correct place in the list. To make this easier you
could store the name of the current file inside the placeholder file.

But I'm of the opinion, if it works and doesn't cause you pain every time
you have to make a change, stick with it. :-)

regards,
Wim


More information about the Techtalk mailing list