[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Replace the last item in a string of 'words'
- From: Paul K <paul@...>
- Date: Wed, 26 Jun 2019 11:26:54 -0700
Hi Russ,
> local l = line:gsub("%s+$", "newval"..i)
It should be uppercase S, as `%S+$` will match any non-witespace
characters at the end of the string. From PiL
(https://www.lua.org/pil/20.2.html): "An upper case version of any of
those classes represents the complement of the class. For instance,
'%A' represents all non-letter characters."
I'm not sure why the %S+ match didn't work for you, but I tested on
the exact lines you used and it worked as expected.
Paul.
On Wed, Jun 26, 2019 at 11:17 AM Russell Haley <russ.haley@gmail.com> wrote:
>
>
>
> On Wed, Jun 26, 2019 at 10:49 AM Paul K <paul@zerobrane.com> wrote:
>>
>> Hi Russ,
>>
>> > Given a file of lines in a file like below, what would be the best way to replace the last item on each line (e.g. V6 in the last line)?
>> > twa01.dat 16 2000 16 0 5 -29501 0 V6
>>
>> Why not `:gsub("%S+$", "newval")`?
>>
>> Paul.
>>
> Hi Paul. To answer your question: because I have no idea what I'm doing. I really suck at string manipulation in Lua and I'm equally poor in regex in any other language. I tried it as you've written it with no matches. The Lua reference manual doesn't seem to indicate that capital %S does anything (but I could be missing something). I changed it to a lowercase %s and got closer to what I need:
>
> local i = 0
> for line in io.lines(file_name) do
> if line:match('.dat') then
> i = i + 1
> local l = line:gsub("%s+$", "newval"..i)
> print(l)
> end
> end
> end
>
> results in:
> russellh@canary-dev:~/physionet/get_stats$ ./modify_headers.lua --rec twa01
> twa01.dat 16 2000 16 0 12 10980 0 Inewval1
> twa01.dat 16 2000 16 0 14 -9048 0 IInewval2
> twa01.dat 16 2000 16 0 1 -25727 0 IIInewval3
> twa01.dat 16 2000 16 0 -14 29120 0 aVRnewval4
> twa01.dat 16 2000 16 0 5 15064 0 aVLnewval5
> twa01.dat 16 2000 16 0 8 17036 0 aVFnewval6
> twa01.dat 16 2000 16 0 2 19694 0 V1newval7
> twa01.dat 16 2000 16 0 12 26289 0 V2newval8
> twa01.dat 16 2000 16 0 21 -23938 0 V3newval9
> twa01.dat 16 2000 16 0 18 11347 0 V4newval10
> twa01.dat 16 2000 16 0 6 27591 0 V5newval11
> twa01.dat 16 2000 16 0 5 -29501 0 V6newval12
>
> So, it's close but not removing the original value?
>
> Thanks for your help (And thank you to Gabriel as well!),
> Russ
>
>>
>> On Wed, Jun 26, 2019 at 10:45 AM Russell Haley <russ.haley@gmail.com> wrote:
>> >
>> > Hi,
>> >
>> > Given a file of lines in a file like below, what would be the best way to replace the last item on each line (e.g. V6 in the last line)?
>> >
>> > twa01.dat 16 2000 16 0 12 10980 0 I
>> > twa01.dat 16 2000 16 0 14 -9048 0 II
>> > twa01.dat 16 2000 16 0 1 -25727 0 III
>> > twa01.dat 16 2000 16 0 -14 29120 0 aVR
>> > twa01.dat 16 2000 16 0 5 15064 0 aVL
>> > twa01.dat 16 2000 16 0 8 17036 0 aVF
>> > twa01.dat 16 2000 16 0 2 19694 0 V1
>> > twa01.dat 16 2000 16 0 12 26289 0 V2
>> > twa01.dat 16 2000 16 0 21 -23938 0 V3
>> > twa01.dat 16 2000 16 0 18 11347 0 V4
>> > twa01.dat 16 2000 16 0 6 27591 0 V5
>> > twa01.dat 16 2000 16 0 5 -29501 0 V6
>> >
>> > I'm not even having much luck with simply capturing the separate items. I've tried a bunch of variations on this:
>> >
>> > for line in io.lines(file_name) do
>> > if line:match('.dat') then
>> > local t = {}
>> > for v in line:gmatch('%s(.)') do
>> > t[#t+1] = v
>> > end
>> > for i,v in pairs(t) do
>> > print(i,v)
>> > end
>> > end
>> > end
>> >
>> > Any advice would be appreciated.
>> >
>> > Thanks!
>> > Russ
>>