lua-users home
lua-l archive

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


With the whole file, if it doesn't contain any null characters, I
would use the frontier pattern: file_contents:gsub('%S+%f[\n\0]',
something) in Lua 5.3 or 5.2 and file_contents:gsub('%S+%f[\n%z]',
something) in Lua 5.1. With a single line, line:gsub('%S+$',
something) as mentioned by Paul.

— Gabriel

On Wed, Jun 26, 2019 at 12:49 PM 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.
>
> 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
>