7 ms·
The reason these sequences of commands always start with cat, for me at least, is that I just cat’d the file only to find it was too long or noisy. cat fil
by bee_rider 3y ago
The reason these sequences of commands always start with cat, for me at least, is that I just cat’d the file only to find it was too long or noisy.
cat filename.txt
Up | grep “thing I want”
Is fewer keystrokes than
cat filename.txt
grep “thing I want” filename.txt
Or more likely
cat filename.txt
grep filename.txt “thing I want”
grep “thing I want” filename.txt
- deleted 3y ago[deleted]
- hnlmorg 3y agoYou can also do: grep “thing I want” !$ Bash (and similar) will replace !$ with the last parameter of the previous command. This is a trick I’ve used lots when wanting to perform a non-piping operation on a command I’ve ‘cat’ed (eg ‘rm -v !$’) I’d never criticise anyone for “useless” use of ‘cat’ though. If the fork() overhead was really that critical then it wouldn’t be a shell command to begin with.
- mmh0000 3y agoEven easier, press alt+. (fewer keystrokes too! cli golf is fun), it'll copy-and-paste the last parameter from the previous command. If you press . multiple times it'll go further back into your history.
- bee_rider 3y agoOften the shortcuts that people point out in these threads are too niche for me to remember, but that seems like a pretty great one, I’ll try to remember it.
- imp0cat 3y agoalt + . is easy to remember, but did you know you there is a way to recall any argument from the previous command in bash? Press Escape, then the number of the argument from the previous command, confirm with Ctrl+Alt+Y. Example: > command arg1 arg2 arg3 Escape, 1, Ctrl+Alt+Y gives you arg1.
- penguin_booze 3y agoalt + <number> + alt + . should do the same thing. No need for ctrl+alt+y, is there?
- smackeyacky 3y agoNot sure that would work everywhere but !$ definitely does for retrieving the last arg of the last command. 40 years of muscle memory right there
- bierjunge 3y agoNobody mentions $_ ? It gives you the last argument used, so: cat filename.txt grep "what I want" $_ expands "$_" to "filename.txt"
- penguin_booze 3y agoAlt + . is a readline binding, so that's available wherever GNU readline is used. Immediate examples that come to mind are Python PDB shell and GDB.
- cassepipe 3y agoAnd thus you get vim modes there too much if you set your .inputrc for it !
- 6c696e7578 3y agoAs I tend to live with set -o vi, [esc] _ gets the same. I hardly ever remember to set: bind '"\e."':yank-last-arg So [esc] _ does it on systems where I haven't customised my environment. A major drawback though is it doesn't go through history like alt . does. However, when 'set -o vi' is enabled you can easily go through history and edit with familiar vi keystrokes, or press 'fc' and fix the previous command in EDITOR, or 'v' to edit the current command in EDITOR.
- cassepipe 3y agoCommand line vi modes are my bread and butter I think you meant 'vv' and I wonder if it's not something that's set up by oh-my-zsh ? It's great though
- 6c696e7578 3y ago'v' will start the visual editor. 'fc' will edit the previous command. Both invoke EDITOR in bash's vi readline command mode. I wasn't aware that bash and zsh did it differently, I assumed they'd both use the same readline - now I'm aware there's more to understand about it :)
- cassepipe 3y agoHa my bad I assumed zsh because I saw the `bind` command being used. I did not know that it was also in bash I find that zsh's mode is actually better than readline's For example it can handle visual mode which is why the same function is `vv` and not just `v` Zsh can handle text objects too, very useful to be able to ci" for example Fish's can too and is quite good IIRC Too bad the `v` command does not work in gdb so it seems it's more of a bash thing than a readline thing Relevant : https://superuser.com/questions/1543120/make-readline-edit-input-command-in-external-editor-similar-behavior-as-edit-a https://superuser.com/questions/1543120/make-readline-edit-i...
- bumblebeast 3y agoI just use <esc>. On the command line to bring back the last argument. Then I can look at it before hitting return
- harrisi 3y agoshopt -s histverify
- dllthomas 3y ago<!$ grep ... or <$_ grep ... work, too.
- bee_rider 3y agoI’m sure there are cases where the fork overhead matters. But, alas, I don’t type or read that fast.
- deleted 3y ago[deleted]
- 77pt77 3y ago!! is the previous command line BTW useful shorthand > sudo !! for example. Again, bash specific.
- emmelaich 3y ago!!, !$ etc are old and supported by zsh as well.
- taskforcegemini 3y agomy preference for this example is "arrow-up, ctrl+a sudo" but will not work inside "screen" out of the box of course
- sophacles 3y agoI look through a lot of logs, so I've aliased `not` to `grep -vE` and using the method you describe over several iterations i end up with a history that has a lot of cat log | not spam1 | not spam2 | not 'spam(3|4)' | .... | less
- mmh0000 3y agoI've been using Linux for a very long time and never thought of that as an alias. I love it! Thank you for sharing.
- kqr 3y agoI really like that alias. Thanks!
- lambdaba 3y agoZsh global aliases can be nice for commands commonly used in pipelines: alias -g V='| grep -vE' alias -g L="| $PAGER" Then you can do cat log V spam1 V spam2 L I also like alias -g G='| grep' alias -g X='| xargs' The possibilities are endless...
- TylerE 3y agoSo are the injection possibilities. That makes me very nervous.
- lambdaba 3y agoWhat's the scenario?
- TylerE 3y agoSomething sets $PAGER to “rm -rf /“ or something along those lines. Really I’d be more worried about accidental invocations. Aliases are not scoped, so if you’re dealing with one of those programs with non standards option handling and uppercase single character switches…
- deathanatos 3y agoI've always though UUoC while working interactively are fine. One shouldn't interrupt their flow to fix minutia in a one time command. UUoC criticism, to me, belongs when one sits down to script.
- 0cf8612b2e1e 3y agoEventually I will want to debug/change the script where we get right back to the situation where I want cat at the front of the line.
- neuromanser 3y agoPress alt and dot (full stop) to insert last word from the previous command line: $ cat file $ grep stuff alt-. Alternatively, make use off the READNULLCMD mechanism in Zsh: $ < file translates to $ ${READNULLCMD:-more} < file Thus you can $ < file then UP (or ctrl-p which I find more ergonomic) and continue with "grep stuff": $ < file grep stuff (Redirections can be anywhere in the command.) https://zsh.sourceforge.io/Doc/Release/Redirection.html#Redirections-with-no-command https://zsh.sourceforge.io/Doc/Release/Redirection.html#Redi...
- hawski 3y agoTry: < file.txt grep pattern Less keystrokes.
- FreeFull 3y agoThat doesn't work for the first step though, where you want to print the file to stdout without processing it in any way
- hawski 3y agoFair. One could get used to the following for the first step: < file.txt cat But then one has to ctrl-w cat. It is a pity that this is not an alternative to cat for a single file: < file.txt
- skipkey 3y agoOr what I find myself doing often: cat “filename.txt” Up | grep “thing” Up | grep -v “not thing” Up | grep -v “other thing” Etc. it’s just easier to build this way even if the initial cat is unnecessary.
- mdekkers 3y ago> grep filename.txt “thing I want” > grep “thing I want” filename.txt. …every time