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

Re: How to save filtered less results in a file or on stdout



On Wed 07 Apr 2021 at 08:30:30 (+0000), Curt wrote:
> On 2021-04-07, David Wright <deblis@lionunicorn.co.uk> wrote:
> >> >
> >> > But I don't know how vim would do on-the-fly filtering like
> >> > less can do with & (not being very familiar with vim).
> >> 
> >>  :g/pattern/.w! >> output.txt 
> >> 
> >> I pressed 'v' in less, searched a pattern and wrote the filtered output
> >> to a file using the method above. Seemed to work except the results of
> >> the filtering are not visualized using this method. Of course, you can
> >> visualize them prior to writing the results to a file (:g/pattern).
> >> Maybe a little fastidious.
> >
> > OK, you "pressed 'v' in less", but what was your command line?
> > Without that, it's difficult to replicate your actions.
> 
> I opened a text file in less, pressed 'v' to invoke my default editor (vim),
> filtered for *pattern* and wrote the filtered output to a file 
> (':g/pattern/.w!>> output.txt'), then quit vim and was returned to 
> less again.

There's the difference, then. In your case, most people would
just edit the file by typing   vim filename   rather than
less filename   and then  v.

More of a challenge is the example I gave, since snipped, where
the input has been piped into less, so there is no file to edit.
Here, again, with vim:

  $ sort -m /var/log/kern.log /var/log/user.log | EDITOR=vim less

  Pressing v gives me:

      Cannot edit standard input  (press RETURN)

> Maybe I'm off the mark here. I thought this was what the OP wanted
> to do (but natively, as it were; however, I found no method of
> doing this with less' native commands).

Strictly, neither could I, the problem being that filtering only
affects what you see, and less logs whatever input is *read*.
(There's a hint: when you filter, the line numbers and percentages
displayed are still tied to the input lines, not just the filtered
ones.)

So the best I could manage was to use grep through the shell escape,
to repeat the filtering that the OP found satisfactory in less.
And even that required copy/paste to transfer less's filter to
grep's command line. A word of warning on that: if you habitually
use case-insensitive searching in less (as I do), you may need to
add -i to the grep command to match the pasted string.

Cheers,
David.


Reply to: