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

Miriam English mim at miriam-english.org
Mon Feb 25 13:22:13 UTC 2013


Thanks for the thoughts Wim.

I've just been looking through some of my old manuals and I found the 
command I had half-remembered. It is the ARexx command COMPARE() from my 
old Amiga days. It compares two strings and returns the first position 
in which they differ, or 0 if they are identical.

ARexx is a surprisingly powerful language when working with strings, 
especially in an Amiga environment, where it has privileged access to 
the internals of virtually all Amiga applications (all functions and 
menu items in most programs have matching ARexx commands) so it works as 
a kind of high-level macro language, allowing simple scripts to be 
written that connect together all sorts of things (wordprocessors, 
spreadsheets, paint programs, terminals, etc) in very cool ways. I still 
have my Amigas, but rarely use them nowadays. [sigh] Today belongs to 
Linux. One step backwards and two steps forward.

There are several active projects out there developing Rexx, but without 
the capabilities of inter-program communication it has on the Amiga, 
Rexx loses much of its appeal for me on Linux.

And as you said, "if it works and doesn't cause you pain every time you 
have to make a change, stick with it."

Best wishes,

	- Miriam

Wim De Smet wrote:
> 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
> _______________________________________________
> Techtalk mailing list
> Techtalk at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/techtalk
>
>

-- 
If you don't have any failures then you're not trying hard enough.
  - Dr. Charles Elachi, director of NASA's Jet Propulsion Laboratory
-----
Website: http://miriam-english.org
Blogs:   http://miriam-e.dreamwidth.org
          http://miriam-e.livejournal.com




More information about the Techtalk mailing list