lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


`%S` stands for "non-space char" and `%s` stays for "space char".
Quoting reference:

> For all classes represented by single letters (%a, %c, etc.), the
corresponding uppercase letter represents the complement of the class.
For instance, `%S` represents all non-space characters.

Looks like your strings have some space characters at the ending. If
so, this should probably work:

local i = 0
for line in io.lines(file_name) do
    if line:match('.dat') then
        i=i+1
        print(line:gsub("(%S+%s*)$", "newval"..i))
    end
end

Or, if you must keep space characters at the end of lines:

...
        print(line:gsub("(%S+)%s*$", "newval"..i))
...

On Wed, 2019-06-26 at 11:16 -0700, Russell Haley 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
> > 
-- 
v <v19930312@gmail.com>