Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

112 views
Skip to first unread message

l0f...@tuta.io

unread,
Mar 28, 2021, 2:40:04 PM3/28/21
to
Hi,

Strictly speaking, my question is not on Debian itself, but on usage of less command running on my Buster (487-0.1+b1).

Is there a way to save *filtered* less results in a file or on stdout please?

What I mean by "filtered" is results obtained via "&" or "&!" command inside less.

Indeed, commands like "|", "-o" or "s" inside less don't seem to take filtering into account...

Thanks in advance :)

Best regards,
l0f4r0

David Wright

unread,
Mar 28, 2021, 3:30:04 PM3/28/21
to
On Sun 28 Mar 2021 at 20:38:08 (+0200), l0f...@tuta.io wrote:
>
> Strictly speaking, my question is not on Debian itself, but on usage of less command running on my Buster (487-0.1+b1).
>
> Is there a way to save *filtered* less results in a file or on stdout please?
>
> What I mean by "filtered" is results obtained via "&" or "&!" command inside less.
>
> Indeed, commands like "|", "-o" or "s" inside less don't seem to take filtering into account...

When you've used
& pattern
to display lines containing your pattern, then
| . grep pattern > filename
will apply the same filter to the lines selected by | ., where
I've used . but you could use ^ or $ or a mark instead, as
appropriate.

Cheers,
David.

l0f...@tuta.io

unread,
Mar 28, 2021, 6:50:04 PM3/28/21
to
Hi David and thanks for your feedback.

28 mars 2021, 21:27 de deb...@lionunicorn.co.uk:

> When you've used
> & pattern
> to display lines containing your pattern, then
> | . grep pattern > filename
> will apply the same filter to the lines selected by | ., where
> I've used . but you could use ^ or $ or a mark instead, as
> appropriate.
>
Indeed, I had noticed that (and even placed myself at the very beginning of the input and used "| $" instead in order to select everything).

However, I was wondering if there was an alternative when one doesn't need to reenter the same pattern twice for such a basic need...

No easy condensed way for that? :)

Best regards,
l0f4r0

songbird

unread,
Mar 28, 2021, 7:10:04 PM3/28/21
to
if you are going to do something to the contents of
a file you can use:

$ grep "pattern" filename > output

there are different versions of grep command (egrep and
fgrep too besides just grep - check the man pages).

if you are taking the input to grep from some other
command you can pipe the output of that command to the
grep command and then redirect what comes out of that
into a file:

$ command | grep pattern > outputfile

less and more are what most would consider pager
programs and not really all that oriented towards
filtering like grep.

for more complicated kinds of output processing
you can use awk/gawk or many other things available.

i never much got into perl.

i think more modern peoples use perl instead of
grep/awk/etc. :)


songbird

l0f...@tuta.io

unread,
Mar 28, 2021, 7:30:08 PM3/28/21
to
Songbird,

29 mars 2021, 00:55 de song...@anthive.com:

> if you are going to do something to the contents of
> a file you can use:
>
> $ grep "pattern" filename > output
>
> there are different versions of grep command (egrep and
> fgrep too besides just grep - check the man pages).
>
> if you are taking the input to grep from some other
> command you can pipe the output of that command to the
> grep command and then redirect what comes out of that
> into a file:
>
> $ command | grep pattern > outputfile
>
> less and more are what most would consider pager
> programs and not really all that oriented towards
> filtering like grep.
>
> for more complicated kinds of output processing
> you can use awk/gawk or many other things available.
>
> i never much got into perl.
>
> i think more modern peoples use perl instead of
> grep/awk/etc. :)
>
Actually, I asked this question because of my workflow here.

First, I use the less pager to navigate quickly inside an input, not knowing beforehand what it looks like and what I will need to do.
Then, I filter some lines depending on my own investigation.
And finally, I'm thinking that it would be too bad to lose the remaining lines so I'm looking for a way to save them eventually.

Of course, you're right, if I knew beforehand what to look for, there are more appropriate and advanced tools (even sed)! :)

Best regards,
l0f4r0

David Wright

unread,
Mar 28, 2021, 9:30:04 PM3/28/21
to
It's not a workflow that I'm in the habit of using, but anyway, having
come up with a pattern that's producing the lines you're interested in,
I would:

. type & again,
. UpArrow to recall this latest pattern that you're happy with,
. LeftDrag the mouse to copy the pattern,
. Backspace to rubout the line (the & remains there),
. type | and respond appropriately to the mark: prompt,
. type your shell command:
. grep '
. paste the pattern with whichever button you use
. ' > filename

Using the ' quote will cater for most patterns, though you
might have to adjust for any backslashes in the pattern,
and so on, using Left/RightArrow, or even use " if the
pattern itself contains ' characters.

I tend to paste with Shift-Insert (needs less hand-eye coordination).

Cheers,
David.

l0f...@tuta.io

unread,
Apr 4, 2021, 5:50:04 AM4/4/21
to
Hi David,

29 mars 2021, 03:25 de deb...@lionunicorn.co.uk:

> . type & again,
> . UpArrow to recall this latest pattern that you're happy with,
> . LeftDrag the mouse to copy the pattern,
> . Backspace to rubout the line (the & remains there),
> . type | and respond appropriately to the mark: prompt,
> . type your shell command:
> . grep '
> . paste the pattern with whichever button you use
> . ' > filename
>
> Using the ' quote will cater for most patterns, though you
> might have to adjust for any backslashes in the pattern,
> and so on, using Left/RightArrow, or even use " if the
> pattern itself contains ' characters.
>
> I tend to paste with Shift-Insert (needs less hand-eye coordination).
>
It works indeed.

I just thought there was an immediate, simple and dedicated command for that.
I suppose everyone's needs/workflows are different ;)

Thanks & Best regards,
l0f4r0

Andrei POPESCU

unread,
Apr 5, 2021, 10:50:04 AM4/5/21
to
On Lu, 29 mar 21, 01:20:18, l0f...@tuta.io wrote:
>
> Actually, I asked this question because of my workflow here.
>
> First, I use the less pager to navigate quickly inside an input, not knowing beforehand what it looks like and what I will need to do.
> Then, I filter some lines depending on my own investigation.
> And finally, I'm thinking that it would be too bad to lose the remaining lines so I'm looking for a way to save them eventually.

If you are willing to learn (neo)vim, '(n)vim -R' as 'view' can be used
instead of 'less', with all the power of an advanced editor at your
disposal.

Kind regards,
Andrei
--
http://wiki.debian.org/FAQsFromDebianUser
signature.asc

jr

unread,
Apr 5, 2021, 8:30:04 PM4/5/21
to
on Mon, 5 Apr 2021 17:44:28 Andrei POPESCU wrote:
> If you are willing to learn (neo)vim, '(n)vim -R' as 'view' can be used
> instead of 'less', with all the power of an advanced editor at your
> disposal.

or just press 'v' in 'less'?

--
regards, jr.

You have the right to free speech, as long as you're not dumb enough
to actually try it. (The Clash 'Know Your Rights')

David Wright

unread,
Apr 5, 2021, 11:30:04 PM4/5/21
to
On Tue 06 Apr 2021 at 01:09:09 (+0100), jr wrote:
> on Mon, 5 Apr 2021 17:44:28 Andrei POPESCU wrote:
> > If you are willing to learn (neo)vim, '(n)vim -R' as 'view' can be used
> > instead of 'less', with all the power of an advanced editor at your
> > disposal.

… or '| vim -R -' or even '| vim -' in place of '| less'.

But I don't know how vim would do on-the-fly filtering like
less can do with & (not being very familiar with vim).

> or just press 'v' in 'less'?

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

Pressing v gives me:

Cannot edit standard input (press RETURN)

Cheers,
David.

Andrei POPESCU

unread,
Apr 6, 2021, 1:40:04 AM4/6/21
to
On Lu, 05 apr 21, 22:29:31, David Wright 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).

A quick web search suggests:

:v/pattern/d

press 'u' to undo or ':w filename.txt' to write the result to a
different file.
signature.asc

Curt

unread,
Apr 6, 2021, 5:40:04 AM4/6/21
to
On 2021-04-06, David Wright <deb...@lionunicorn.co.uk> wrote:
> On Tue 06 Apr 2021 at 01:09:09 (+0100), jr wrote:
>> on Mon, 5 Apr 2021 17:44:28 Andrei POPESCU wrote:
>> > If you are willing to learn (neo)vim, '(n)vim -R' as 'view' can be used
>> > instead of 'less', with all the power of an advanced editor at your
>> > disposal.
>
> … or '| vim -R -' or even '| vim -' in place of '| less'.
>
> 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.

>> or just press 'v' in 'less'?
>
> $ sort -m /var/log/kern.log /var/log/user.log | EDITOR=emacs less
>
> Pressing v gives me:
>
> Cannot edit standard input (press RETURN)


> Cheers,
> David.


--

David Wright

unread,
Apr 6, 2021, 11:50:04 PM4/6/21
to
On Tue 06 Apr 2021 at 09:12:45 (+0000), Curt wrote:
> On 2021-04-06, David Wright <deb...@lionunicorn.co.uk> wrote:
> > On Tue 06 Apr 2021 at 01:09:09 (+0100), jr wrote:
> >> on Mon, 5 Apr 2021 17:44:28 Andrei POPESCU wrote:
> >> > If you are willing to learn (neo)vim, '(n)vim -R' as 'view' can be used
> >> > instead of 'less', with all the power of an advanced editor at your
> >> > disposal.
> >
> > … or '| vim -R -' or even '| vim -' in place of '| less'.
> >
> > 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.

David Wright

unread,
Apr 6, 2021, 11:50:04 PM4/6/21
to
On Tue 06 Apr 2021 at 08:34:24 (+0300), Andrei POPESCU wrote:
> On Lu, 05 apr 21, 22:29:31, David Wright 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).
>
> A quick web search suggests:
>
> :v/pattern/d
>
> press 'u' to undo or ':w filename.txt' to write the result to a
> different file.

That works fine; as you say, one has to learn the editor concerned,
or at least learn some tricks that aren't always as straightforward
in an editor as in less.

Cheers,
David.

Curt

unread,
Apr 7, 2021, 5:00:05 AM4/7/21
to
On 2021-04-07, David Wright <deb...@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.

curty@einstein:~$ cat son.txt

J'ai négligé de lire le encore très volumineux--si j'ose dire--fils,
mais j'ai remarqué depuis le début (un début perdu dans l'obscurité
croissante du passé) le même phénomène chez moi que vous décrivez.

Il semble qu'une différence de gamme dynamique entre les deux supports
(plus large en ce qui concerne le mini par rapport à la TNT et donc
donnant l'impression d'être moins fort dans le milieu de plage) pourrait
expliquer l'effet constaté.

Si toutefois mon hypothèse de départ est vraie !

curty@einstein:~$ less son.txt
v
:g/décrivez/.w! >> output.txt
:q
now I'm back in less looking at son.txt
q

curty@einstein:~$ cat output.txt
croissante du passé) le même phénomène chez moi que vous décrivez.

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).

David Wright

unread,
Apr 7, 2021, 12:50:04 PM4/7/21
to
On Wed 07 Apr 2021 at 08:30:30 (+0000), Curt wrote:
> On 2021-04-07, David Wright <deb...@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.

l0f...@tuta.io

unread,
Apr 11, 2021, 2:40:04 PM4/11/21
to
Hi,

Thank you everyone for your last replies.

Everything you suggested works as expected.

Bottom line:

* less: no quick/immediate way (i.e. like a shortcut) to save only the filter output.
One needs to pipe through `grep` with the same filter(s) in `less` shell

* vim (but it certainly works for other editors as well): indeed, this kind of operation is "basic" for such an advanced editor ;)
One just needs to remember how to do that (I think there is no other way to learn vim than using it everyday otherwise one forgets rapidly)...
I like vim a lot but I don't really know why but sometimes I like `less` simplicity (i.e. few things to remember) hence my initial question :)

NB in vim: `v/pattern/d` = `g!/pattern/d`

Thanks again!

Best regards,
l0f4r0
0 new messages