6 ms·
If I had a nickel for each `cat foo.json | jq | pbcopy`, I'd be a rich man :)
by omginternets 3y ago
If I had a nickel for each `cat foo.json | jq | pbcopy`, I'd be a rich man :)
- maleldil 3y agoThat's a useless use of cat. You can use `jq . foo.json | pbcopy` or `jq < foo.json | pbcopy`.
- jdbartee 3y agoSpeaking for myself, the first form is more natural- even if it’s a useless cat, because I’m always cat-ing files to see their structure. Then progressively tacking on different transforms. And then finally putting it in whatever I want as output. It’s so ingrained, I’m more likely than not to just write it out that way even when I know exactly what I’m doing from the onset.
- paulddraper 3y agoYou could consider < foo.json jq | pbcopy
- jonnycomputer 3y agoYes, this iterative procedure is often why "useless" cats get put into it. It's a very effective way of processing regular text information. e.g. I need to grab some info from textfile.txt to use as arguments to a function. cat textfile.txt looks like its comma delimited. cat textfile.txt | cut -d, -f 2-5 ah, its the third and fourth column i need cat textfile.txt | cut -d, -f 3-4 | grep '123456' perfect cat textfile.txt | cut -d, -f 3-4 | grep 123456 | tr , ' ' myfunc $(cat textfile.txt | cut -d, -f 3-4 | grep 123456 | tr , ' ')
- gumby 3y ago> cat textfile.txt > looks like its comma delimited. Interesting; why wouldn't you use `head`? Who knows how big textfile.txt is?
- rovr138 3y ago`file` will tell you too
- jonnycomputer 3y agoWon't tell you the delimiter.
- deleted 3y ago[deleted]
- yrro 3y agoDon't forget to pipe head into 'cat -v'... that text file could contain _anything_!
- gunapologist99 3y agoThank you for pointing this out! This is much safer.
- lelandbatey 3y agoI really recommend folks use "less" over cat, especially keyboard oriented folks. Different terminal emulators don't always have the scroll behavior I want, not do they always allow me to search the file I'm looking at. "less" does all those things, in nearly every environment no matter the terminal emulator, and has other wonderful options to boot (chop long lines so they don't wrap can be nice for logs, line numbers can be VITAL, etc). I still uselessly use cat though, it's such a nice way to build a pipeline.
- jamespullar 3y agoI've been using bat as a cat replacement for a while now. It includes paging, syntax highlighting, line numbers, and is generally very performant. https://github.com/sharkdp/bat https://github.com/sharkdp/bat
- patrec 3y agoIf you're using zsh, you can just replace any instance of $ cat somefile ... with $ <somefile ... For bash, this only works if you have at least one `|`.
- ddingus 3y agoI did this last time I saw it come up and was surprised! Doing it makes perfect sense in hindsight. Neato!
- fastaguy88 3y agoAs a scientist who cares about reproducibility, the big difference between the "useless cat" and providing the input file name on the command line is that, in the latter case, the program can capture that file name and reproduce it. That is harder when using stdin. Many of my programs and scripts start output with the line: # cmd arg1 arg2 arg3 ... and simply echo back lines that start with '#'. That way, I have an internal record of the program that was run and the data file that was read (as well as previous parts of the analysis chain). And, 'R' ignores lines starting with '#', so the record is there, but does not affect later analyses.
- nicky0 3y agoIn what way do you see those alternatives as superior?
- paulddraper 3y agoThey avoid an unnecessary invocation of the cat executable. Instead, they open a file descriptor and pass that. Tiny difference but there you go.
- latexr 3y agoTo add, searching for “useless use of cat” will yield several results for those interested in learning more. Other examples include “useless use of echo” and “useless use of ls *”.
- vram22 3y agoYes. Even the Wikipedia page on cat has a section about that, titled eponymously. https://en.m.wikipedia.org/wiki/Cat_(Unix) https://en.m.wikipedia.org/wiki/Cat_(Unix)
- adrianmonk 3y agoNot just that, but also all the bytes have to go through an extra pipe. Presumably they're copied an extra time because of this. When you run "cmd < file", the command reads from stdin, which pulls directly from the file. When you do "cat file | cmd", "cat" opens the file, reads from there, and writes to a pipe. Then "cmd" reads from its stdin, which is a pipe.
- chlorion 3y agoGNU cat will use the copy_file_range syscall when possible! copy_file_range allows a user land program to copy data between two files without doing any user space work. Instead of reading data into a buffer and writing it back out to the destination, the kernel will somehow manage to move the data for you. I think this will prevent any extra copies from occurring in situations where it can be used. https://man.archlinux.org/man/copy_file_range.2 https://man.archlinux.org/man/copy_file_range.2 https://git.savannah.gnu.org/cgit/coreutils.git/tree/src/cat.c#n507 https://git.savannah.gnu.org/cgit/coreutils.git/tree/src/cat...
- deleted 3y ago[deleted]
- nojs 3y agoThe “useless cat” meme needs to die. Everyone is aware that most commands accept a file argument, but looking up the arguments and their ordering is annoying and using cat for things like this is just fine.
- omginternets 3y agoThis. "Useless cat" is more useful than "useless file-arg".
- epcoa 3y agoThe redirect always works though - that is not a program argument, that is handled by the shell. Apparently not everyone is aware of that.
- hdb2 3y agogranted, it is a little snarky and maybe the snark isn't appropriate in today's tech environment. but no, things like "useless use of cat" do not need to go away, because they make me better at what I do in little ways. those little ways add up over time. > but looking up the arguments and their ordering is annoying you seem to be arguing for complacency. taking your idea to an extreme, why learn to do _anything_ well?
- burnished 3y agoEveryone is not aware, new people are joining all the time.
- Someone 3y agoIs there any shell that has cat as a built-in? Such a shell could remove some of the more common cases.
- hnlmorg 3y agoAll of them do. Including bash. It’s just not the same syntax (ie ‘< filename’). But I honestly think people who try to optimise away ‘cat’ are optimising the wrong thing. If one extra fork() is that detrimental then don’t use a shell scripting language. For a lot of people, “useless” ‘cat’ enables them to write a pipeline in the order that their brain farts out the requirements for the pipeline. So they’ve optimised for human productivity. And given the human brain is slower than a few extra fork()s, I think optimising for one’s brain makes more sense here.
- Someone 3y ago> All of them do. Including bash. Are you sure? https://unix.stackexchange.com/questions/208615/is-cat-a-shell-built-in-or-an-external-program https://unix.stackexchange.com/questions/208615/is-cat-a-she... disagrees and neither https://manpages.ubuntu.com/manpages/jammy/man7/bash-builtins.7.html https://manpages.ubuntu.com/manpages/jammy/man7/bash-builtin... nor https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Command... mention it
- hnlmorg 3y agoLiterally the next sentence after the one you quoted explains my point: > It’s just not the same syntax (ie ‘< filename’). Reading from a file isn’t a hard problem. Having a good UX for doing that is where most shells fall apart. And that’s basically what ‘cat’ offers here: an improved UX. Having ‘cat’ as a shell builtin wouldn’t really solve the complaints raised by “useless use of” anyway because you’d still be piping (and in some cases, fork()ing too). You couldnt really use ‘cat’ as syntactic sugar for ‘<‘ because things start to get really weird if you want to pass flags to ‘cat’ or even redirect the output to something other than a pipe. And given ‘cat’ is POSIX (https://en.m.wikipedia.org/wiki/List_of_Unix_commands#/media/File%3APOSIX_Utilities.pdf https://en.m.wikipedia.org/wiki/List_of_Unix_commands#/media...) the current behaviour of shells is, in my opinion, correct. This is why my own shell has a differently named builtin that approximately serves the purpose of ‘cat’ but for instances when you need the command built into the shell and it can’t just be passing a file handle to the next command (in my case, because i wanted to pass metadata out-of-band as well as the file contents)
- cratermoon 3y agohttps://porkmail.org/era/unix/award https://porkmail.org/era/unix/award