Most active commenters
  • jonnycomputer(3)

←back to thread

2039 points Gadiguibou | 18 comments | | HN request time: 0s | source | bottom
Show context
klausa ◴[] No.36491947[source]
`pbcopy` and `pbpaste` are one of my most-loved in the list.

Dealing with some minified json, switching to iTerm, doing `pbpaste | json_pp | pbcopy` and having a clean output is _so_ nice.

replies(28): >>36492008 #>>36492015 #>>36492028 #>>36492101 #>>36492108 #>>36492229 #>>36492265 #>>36492890 #>>36492953 #>>36493037 #>>36493127 #>>36493336 #>>36493457 #>>36493802 #>>36494023 #>>36494242 #>>36494325 #>>36495379 #>>36495894 #>>36496866 #>>36497033 #>>36497293 #>>36497589 #>>36497973 #>>36498181 #>>36498558 #>>36498586 #>>36535798 #
omginternets ◴[] No.36492265[source]
If I had a nickel for each `cat foo.json | jq | pbcopy`, I'd be a rich man :)
replies(1): >>36492862 #
maleldil ◴[] No.36492862[source]
That's a useless use of cat. You can use `jq . foo.json | pbcopy` or `jq < foo.json | pbcopy`.
replies(6): >>36492960 #>>36493144 #>>36493370 #>>36494558 #>>36495101 #>>36495918 #
1. jdbartee ◴[] No.36492960[source]
Speaking 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.

replies(5): >>36493350 #>>36493487 #>>36494377 #>>36496238 #>>36497947 #
2. paulddraper ◴[] No.36493350[source]
You could consider

    < foo.json jq | pbcopy
3. jonnycomputer ◴[] No.36493487[source]
Yes, 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 , ' ')

replies(1): >>36494503 #
4. jamespullar ◴[] No.36494377[source]
I'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

5. gumby ◴[] No.36494503[source]
> cat textfile.txt

> looks like its comma delimited.

Interesting; why wouldn't you use `head`? Who knows how big textfile.txt is?

replies(4): >>36494862 #>>36494869 #>>36494913 #>>36495260 #
6. rovr138 ◴[] No.36494862{3}[source]
`file` will tell you too
replies(1): >>36494911 #
7. ◴[] No.36494869{3}[source]
8. jonnycomputer ◴[] No.36494911{4}[source]
Won't tell you the delimiter.
9. yrro ◴[] No.36494913{3}[source]
Don't forget to pipe head into 'cat -v'... that text file could contain _anything_!
replies(2): >>36495824 #>>36496116 #
10. jonnycomputer ◴[] No.36495260{3}[source]
generally, speaking, if you don't have an idea of how big the file is, or it would take up too much real-estate on your terminal window, sure. 100%. It was just an example.

lot's of times we sort of know what we are working with, but don't remember the particulars especially

11. gunapologist99 ◴[] No.36495824{4}[source]
Thank you for pointing this out! This is much safer.
12. lelandbatey ◴[] No.36496116{4}[source]
I 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.

replies(2): >>36498355 #>>36499959 #
13. patrec ◴[] No.36496238[source]
If 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 `|`.
replies(1): >>36502079 #
14. fastaguy88 ◴[] No.36497947[source]
As 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.

15. jmhammond ◴[] No.36498355{5}[source]
My useless cat is that I always use `cat file | less` when I could just `less file`.

I've been typing cat for over 25 years. Old habits die hard.

16. rconti ◴[] No.36499959{5}[source]
I hate that when I use `less`, then quit, the output goes away.
replies(1): >>36508860 #
17. ddingus ◴[] No.36502079[source]
I did this last time I saw it come up and was surprised! Doing it makes perfect sense in hindsight. Neato!
18. LgWoodenBadger ◴[] No.36508860{6}[source]
You can run "less -X" for that, but it may have other problems depending on how you use less (e.g. scrolling up, etc.)