Wow, after 25 years of Unix experience, I learned that you can filter output in #less.

Press ampersand (&) and enter a regex to show only lines matching the regex.

Press ampersand (&) and then exclamation mark (!) to apply an inverse filter.

@_xhr_ I ususally use grep to filter text streams, but when I don't, it's because I want to see the surrounding non-matching lines, so slash search (/) in less is much more useful to me
@wolf480pl @_xhr_ I think it was the '-C n' parameter that told grep to output n lines before and after a match
@AndreasEK @_xhr_ yes, but that requires knowing ahead if time how many lines you need. Which you usually don't know when looking through logs for a problrm that you're seeing for the first time.
@wolf480pl @_xhr_
I'm a fan of "tail -n x | head -n y" when stepping through logs and grep -C x if I have an idea what I need, but this less command looks interesting. Maybe pipe it after the first piped command! Go crazy!
@Okanogen @_xhr_ well `less` is a pager, so it only makes sense as the *last* command in the pipeline, to browse the output interactively
@wolf480pl @_xhr_
That's what I meant, but described unclearly.

@wolf480pl For grep, consider using the -C switch (context):

-C NUM, -NUM, --context=NUMPrint NUM lines of output context. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.

@xhr@cybervillains.com

@cweickhmann @_xhr_ yeah I know how -C works, and I use it when it makes sense. But it only works when you know ahead of time how much context you need. I think I already said that somewhere else in the thread but can't find it now because mastodon likes to forget about old posts...
@wolf480pl
Absolutely. (Honestly, I didnโ€™t check if you mentioned it elsewhere. I just thought in that context it may be useful for you or someone else reading the thread ๐Ÿ˜‰)
@xhr@cybervillains.com
@_xhr_ thatโ€™s amazing! (Also amazing: watching other people use almost entirely different subsets of vi commands at speed to my own)
@_xhr_ Oh that is fantastic! Do you by any chance know which flavor of regex it uses? POSIX?

@_xhr_

Sorry, it's too late now, my fingers have been doing

more | grep

on their own now since 1982

@mkarliner @_xhr_ (fellow more-ingrained nerds, Unite!!)

@[email protected]

Also:

/ = search forward
? = search backward

Didn't know the & trick TBH, good one :-)

@[email protected] wow, I never noticed this, and it is even documented. Nice! ๐Ÿ˜Žโ€‹

@_xhr_ woaaaaa, and it does chain!

No more daisy chains of greps, pipes and less at the end o_0

Bunch of future life-years of piping and greping reclaimed for other uses!

Thanks!

@KasTasMykolas what do u mean "it does chain"? @_xhr_

@kouk @_xhr_

&first_filter
&second_filter
&third_filter

repeating the & clicking pattern will continue filtering on already filtered content. Just like grep first_filter | grep second_filter | grep third_filter

@KasTasMykolas Is there a way to undo them? The last one, single ones, or at least all of them?

@kouk @_xhr_

@wonka @kouk @_xhr_ I guess only option is to undo all of them. Which is adding an empty pattern. To quote the man-page:

> If pattern is empty (if you type & immediately followed by ENTER) any filtering is turned off, and all lines are disโ€played.

@_xhr_ I only learned this in the last two years or so as well. Linux user since 1995.

@_xhr_ by the way, for Your defence, it may be not so old addition. For now I can find it crawling into FreeBSD codebase somewhere between 7.3.0 and 7.40:
https://github.com/dspinellis/unix-history-repo/blob/FreeBSD-release/7.4.0/contrib/less/less.man#L260-L274

So, at least 10-11 years ago.

unix-history-repo/less.man at FreeBSD-release/7.4.0 ยท dspinellis/unix-history-repo

Continuous Unix commit history from 1970 until today - unix-history-repo/less.man at FreeBSD-release/7.4.0 ยท dspinellis/unix-history-repo

GitHub
@_xhr_ ok that's a new one for me too

@_xhr_ TIL!

BTW, another neat function (at least in the GNU version?) is pressing Shift-F to make it follow the file (scroll to the end and update when the file changes) - useful for logs!

@Doomed_Daniel @_xhr_ You can also do cat | less +F to follow line immediately. Useful for repeated commands
@_xhr_ @oneiros Didnโ€™t know that either. Thanks.
@_xhr_ another day another "TIL" ๐Ÿ™‚
@_xhr_ I've been doing cat | grep | less for years. This is way better.
@JoeCotellese @_xhr_ I find it more โ€œcat | grep | grep | grep | grep | grep | lessโ€ for me - sometimes I need multiple layers of filtering as Iโ€™m trying to find for specific data.
@talios @JoeCotellese @_xhr_ yeah thatโ€™s me too. 33 years using Unix and I didnโ€™t know this.
@_xhr_ I feel like Iโ€™ve read this exact toot months ago ๐Ÿค”
@_xhr_ I've taken to using @siegel's #BBEdit to opening and receiving pipelines on #macOS. It has nice command line tools to spawn windows for diffing, tailing log files, and parsing results. And its search capabilities are unmatched. https://www.barebones.com/products/bbedit/featuresadmin.html
Bare Bones Software | UNIX & Admin Features

@mjgardner @_xhr_ @siegel oh this is nice. I canโ€™t count the number of times Iโ€™ve copied terminal output into a file in BBEdit to search it.
@_xhr_ Well, I was never any good at regex. But my unix experience is even larger than yours ( 30+ years ), and I am still learning new features.
@wimletzer @_xhr_ Same. I tell myself that itโ€™s my ninja like vi macro skills that took up the space where regex-off-the-cuff would go.
@_xhr_ wow, new to me also. So less is more. More than needed though, the & thingy is against Unix philosophy; there is grep before less. I've given the fact that a pager can go back a lot of thought before deciding its not totally against Unix philosophy.
@_xhr_ This is super cool (and I'm embarrassed I didn't know it) ... BUT https://lnav.org/ basically makes #less obsolete - it's a superb tool which I strongly recommend to all #less users. Check out the features page!
The Logfile Navigator

The Logfile Navigator, lnav for short, is an advanced log file viewer for the small-scale.

The Logfile Navigator
@_xhr_ greetings fellow Villain. In my experience RegEx is both the solution that keeps on giving, and the one that robs me of the most sleep. Itโ€™s a double edged sword, and I wouldnโ€™t have it any other way.
@_xhr_ 30+ years here and I didnโ€™t know that, but Iโ€™m used now to there being an abundance of features in my tools for me to discover. I expect never to fully know what Emacs can do.
@tommythorn
I decided to play on Emacs like on an instrument instead of using it. Feels better now. :)
@_xhr_
@_xhr_ That's a lot more than you expect from less.
@_xhr_ psst! this is only revealed after someone has done 50 years of unix.
@_xhr_ At some point, I learned that in #less, you can press /, followed by the up and down keys to browse your search history.
@_xhr_ Instafollow for combining less and regex ๐Ÿ˜ƒ (even though I never did use less that much, in... 35 years, hardly seems real to say)
@_xhr_ TIL :) And I've been a sysadmin for "only" 12 years. Thanks!

@_xhr_

It's incredibly slow unfortunately :(

I use https://github.com/tigrawap/slit/releases as a much nicer less alternative, with many more features:

See its readme: https://github.com/tigrawap/slit#readme

@finestructure

Releases ยท tigrawap/slit

slit - a modern PAGER for viewing logs, get more than most in less time - tigrawap/slit

GitHub