by ______-_-______ · 4y ago · 0 comments
grep pattern1 **/* | grep pattern2 | grep -v exclude_these | grep -v also_exclude
The problem is this loses filenames and context lines in the output. I want to apply several positive and negative regexes, and only at the very end annotate with filenames and context. Anyone have a good workflow for this?- Interactivity: the results are updated live as you type, so you catch typos sooner and can tweak the search terms as you go.
- The buffer of search results you collect with Embark is not dead text like it would be in the terminal, instead, each line is a link to the corresponding file taking you to the line that matched.
- Wgrep lets you edit all the matching lines in place!
Packages referenced:
1. Consult: https://github.com/minad/consult/
2. Orderless: https://github.com/oantolin/orderless
3. Embark: https://github.com/oantolin/embark
4. Wgrep: http://github.com/mhayashi1120/Emacs-wgrep/raw/master/wgrep....
rg --line-number --no-heading --color=always --smart-case "$@" | fzf -d ':' -n 2.. --ansi --no-sort --preview-window 'down:20%:+{2}' --preview 'bat --style=numbers --color=always --highlight-line {2} {1}'
I get a small preview of the file in the bottom 20% of my terminal, and I can use fzf's matching to further filter, or to exclude thingsI capture the output and then `cut` it up to get filename and line number, which then get used to open the file/line in vim
grep -P '(?=.*pattern1)(?=.*pattern2)(?!.*exclude_these)' **/*
Maybe not the most beautiful or easy to have at the fingertips, but could be worse.Previous discussion here: https://news.ycombinator.com/item?id=20455857
Also featured in that thread: https://github.com/akavel/up
For example: echo '' | fzf --print-query --preview 'grep -R {q} . 2> /dev/null'
perl -ne '( /foo/ and /bar/ and !/baz/ and !/splat/ ) and print "$ARGV: $_"' *
I didn't include the context part, since that would be more than a one-liner, but it's not hard to do in a short script. function jqsk {
sk --tac --ansi --regex --query . --multi --interactive --cmd '{}' \
--bind 'enter:select-all+accept,ctrl-y:select-all+execute-silent(for line in {+}; do echo $line; done | pbcopy)+deselect-all' \
--cmd-history=${HOME}/.sk_history --cmd-history-size=100000 \
--no-clear-if-empty \
--cmd-query "cat $1 | jq --raw-output --color-output --exit-status '.'"
}
I should look into whether fzf or any of the other tools in this thread can be coerced into doing the same thing, the above is pretty verbose and arcane, and it doesn't always behave the way I expect.https://gist.github.com/b0o/f0759c84af6ef773a37a499fdb1c05af
Example: you’re trying to find a file that contains the strings “MIT License” (case insensitive), “npm install”, and a match for “react-.*” somewhere in your home directory:
aag ~ ---- -i "mit license" --- "npm install" --- "react-.*"
The excessive dashes are necessary so that it’s possible to pass different options to each separate invocation of ag.In case it’s not clear why this is useful, normally ag (or grep) searches are linewise. It’s not so easy if you are looking for things that occur on different lines, and possibly in different orders.
grep -inHv -e foo -e bar -e baz ps axjw | awk '(/zsh/ || /login/) && !(/awk/ || /direnv/)'
And you can easily add features like "print the first line unconditionally: ps axjw | awk 'NR == 1 {print} (/zsh/ || /login/) && !(/awk/ || /direnv/)'
IMO, awk is in a pretty uniquely sweet spot between grep and perl/ruby/python.Unfortunately I've not put in the effort to learn awk past printing the n-th column of ls (my typical use) - the extra syntax required to properly 'quote' and {} things puts me off.
Then I highly recommend: emacs / vim buffers
-H File name
-R Recursive (directories)
-n Line number awk '/pattern1/ && /pattern2/ && !/exclude1|exclude2/ {print FILENAME, $0}' */*