News for hackers. Stuff that matters.
Re:New(ish) command line tools by divbzero on Tuesday April 12, 2022 @09:07PM (#31010099)
A simple trick I only figured recently is following logs with fuzzy search:

  tail -f /var/log/foo.log | fzf +s
Or something similar for output from a dev server:

  make serve | fzf --ansi +s
Re:New(ish) command line tools by bofh23 on Tuesday April 12, 2022 @11:19PM (#31010853)
Alternatively, you can use less(1) in tail mode to search growing logs.

  less +F -p pattern /path/to/log
(Ctrl-C to break out of tail mode. /pattern to interactively set search pattern.)
Re:New(ish) command line tools by philsnow on Wednesday April 13, 2022 @02:21AM (#31011742)
Also while inside less: & shows you only lines which match a pattern. You can hit & multiple times and less will show you only those lines which match every input pattern. A ^N right after & negates the pattern. & respects the -I switch (case insensitive pattern matching).

I use this all the time, especially when I'm on a machine that I don't want to bother installing something like fzf on.

Re:New(ish) command line tools by hddqsb on Wednesday April 13, 2022 @03:02AM (#31011928)
Good tips. Small correction: `&` only filters by the most recent pattern.
Re:New(ish) command line tools by philsnow on Wednesday April 13, 2022 @09:52AM (#31014666)
Seems to depend on your particular less, see the last sentence of this paragraph from `man less` on my machine:

       &pattern
              Display only lines which match the pattern; lines which do not
              match the pattern are not displayed.  If pattern is empty (if
              you type & immediately followed by ENTER), any filtering is
              turned off, and all lines are displayed.  While filtering is in
              effect, an ampersand is displayed at the beginning of the
              prompt, as a reminder that some lines in the file may be
              hidden.  Multiple & commands may be entered, in which case only
              lines which match all of the patterns will be displayed.
Indeed, I thought it would behave like you describe.. when I was refreshing my memory of how the negative pattern filtering worked, I first did &/pattern and then &/^Npattern, and was surprised to see that it displayed zero matching lines.

this is my version of less:

  % less --version
  less 581.2 (POSIX regular expressions)
  Copyright (C) 1984-2021  Mark Nudelman
Re:New(ish) command line tools by hddqsb on Wednesday April 13, 2022 @10:16AM (#31014911)
Oh wow, that's a pretty new feature! It was added in v569, 2020[1].

[1]: https://github.com/gwsw/less/commit/6a070fc53799fb86e0fe3880...

Re:New(ish) command line tools by mihaitodor on Tuesday April 12, 2022 @10:08PM (#31010485)
Speaking of logs, angle-grinder is amazing: https://github.com/rcoh/angle-grinder
Re:New(ish) command line tools by francislavoie on Wednesday April 13, 2022 @07:41AM (#31013501)
That looks awesome. Something I'd love help with, I use pdsh to tail logs from multiple servers at once, but the ways I can manipulate the logs feel really limited because of how pdsh works. Does anyone know of a better solution for that? Like, from a head node, aggregate/tail the contents of the same log file on multiple servers. Bonus points if it uses 'genders' too to get the list of servers.
Re:New(ish) command line tools by tstack on Wednesday April 13, 2022 @09:03AM (#31014115)
The Logfile Navigator (https://lnav.org), a TUI for viewing log files, has support for opening logs on remote hosts via SSH in its latest version. See the following post for more information: https://lnav.org/2021/05/03/tailing-remote-files.html

As for integrating with a “genders” host DB, there’s no direct support for it. But, lnav is scriptable, so I’m pretty sure it’s possible to write a script that does what you want. I can help with that if post in the github discussion: https://github.com/tstack/lnav/discussions

Re:New(ish) command line tools by akavel on Wednesday April 13, 2022 @02:40AM (#31011835)
As an alternative allowing the use of any shell command/pipeline on the results interactively, see also: https://github.com/akavel/up
Re:New(ish) command line tools by lytedev on Tuesday April 12, 2022 @09:21PM (#31010197)
I've been using tmux for finding, but I think this might be brilliant and stupid simple. Thank you for this! Never thought of piping "live" output or streaming to fzf!!
Re:New(ish) command line tools by sundarurfriend on Tuesday April 12, 2022 @09:56PM (#31010417)
Could you (or anyone who understands) explain what these do?
Re:New(ish) command line tools by fulafel on Wednesday April 13, 2022 @01:50AM (#31011586)
tail -f will hang around and output new lines that appear in the file.

fzf will receive that input and in this case accumulate the new lines that have come in from tail -f. It will then stay around, present an interactive full screen text ui, and let you interactively filter the set of received lines based on substrings you enter at the fzf prompt. This while continuously incorporating new log lines coming from the pipe, from tail -f. The +s just says not to sort the lines, keeping the matches in the same order they appeared in the logs.

Re:New(ish) command line tools by Alekhine on Tuesday April 12, 2022 @10:35PM (#31010621)
It just takes the last ten lines of a log file, then passes that input to a program that basically lets you search each line. I don't really see the usefulness.

Edit: Ah, I see. According to another comment you see the output of the file as it changes.

Re:New(ish) command line tools by _bohm on Tuesday April 12, 2022 @10:41PM (#31010649)
`tail -f` follows the logfile, so the command allows you to search over the logfile as logs are being added to it
Re:New(ish) command line tools by yewenjie on Tuesday April 12, 2022 @11:41PM (#31010978)
This is amazing! Is it possible to do the following somehow?

- automatically have this for any command that outputs more than 10 lines?

- automatically exit when the main command (for example a dev server) exits?

- print the output of the main command to stdout after this is done?

Re:New(ish) command line tools by faho on Wednesday April 13, 2022 @02:09AM (#31011676)
> automatically have this for any command that outputs more than 10 lines?

Probably not. The output of a command typically goes directly to the terminal and does not pass through the shell, so the shell has no idea how many lines there are.

You could write a shell where that's not the case, but that would have issues with interactive things - what happens if you run e.g. vim or htop in that context?

You can pipe to `less -F` (`--quit-if-one-screen`), but note that the version of `less` shipped with macOS has a bug and might just swallow the output instead.

Re:New(ish) command line tools by efreak on Thursday April 14, 2022 @08:35PM (#31035324)
More likely is a feature to skip processing on short outputs, like `less -F`

``` -F ........ --quit-if-one-screen Quit if entire file fits on first screen. ```

Re:New(ish) command line tools by stirfish on Wednesday April 13, 2022 @01:09AM (#31011399)
Maybe with some clever use of zsh hooks? Here's a gist for zbell; it uses `preexec` and `precmd` to run something right before a command is executed (`preexec`) and right before the prompt is shown again (`precmd`)

https://gist.github.com/jpouellet/5278239

Mine blinks a little light if a command finishes after 30 seconds, and sends me an email if something takes over a minute.

Re:New(ish) command line tools by AmpsterMan on Wednesday April 13, 2022 @02:11AM (#31011686)
fzf is a wonderful little tool. I use this script so much:

git branch | fzf | xargs git checkout

Re:New(ish) command line tools by bloopernova on Wednesday April 13, 2022 @09:05AM (#31014126)
Related git+fzf:

    [alias]
      fza = "!git ls-files -m -o --exclude-standard | fzf -m --print0 | xargs -0 git add"
Then if you run "git fza" you'll get a list of changed files in your repo, which you can use TAB to select/deselect. Hit enter and the selected files will be staged, ready for a commit. Extra cool is that it works from any subdirectory of your repo because it always lists files from the root of the repository. It's really useful for selectively adding lots of files from the command line.

I then alias "ga" to "git fza" for even less typing :)

Re:New(ish) command line tools by omair_inam on Wednesday April 13, 2022 @07:45AM (#31013518)
Excuse my ignorance, but what does this command do?
Re:New(ish) command line tools by _bohm on Wednesday April 13, 2022 @08:51AM (#31014006)
It pipes a list of available git branches into fzf, which lets you filter them by fuzzy string match. When you hit enter, it will take the best matching one and switch to that branch
Re:New(ish) command line tools by omair_inam on Wednesday April 13, 2022 @12:23PM (#31016614)

            _ ._  _ , _ ._
          (_ ' ( `  )_  .__)
        ( (  (    )   `)  ) _)
       (__ (_   (_ . _) _) ,__)
           `~~`\ ' . /`~~`
                ;   ;
                /   \
  _____________/_ __ \_____________
Re:New(ish) command line tools by superasn on Wednesday April 13, 2022 @12:58AM (#31011341)
That's a pretty neat trick. I was using `watch -n1` with grep for this occasionally but this is so much better!
Re:New(ish) command line tools by julius_deane on Wednesday April 13, 2022 @02:40AM (#31011839)

    #.zshrc
    alias -g F=" | fzf --multi"