Live data from Hacker News

Useful use of cat(1)

in-ulm.de

21–30 of 46 posts

Re: Useful use of cat(1)

#21
post #3

cat > file vs. touch file cat file | cmd vs. cmd So even some of the thing listed are still not the best use case for cat

>* cat file | cmd vs. cmd I tend to prefer the cat version as the flow of the command follows the left-to-right direction in which I generally read, and it makes things easier for less experienced users to read and understand for the same reason. It does make things less efficient of course, as there is at least one extra memory-copy of everything and extra process context switching, but in most cases your drives or…

If you want left to right you can do

    

Re: Useful use of cat(1)

#22
post #12
post #5

Instead of using a cat with heredoc, just type cat - | grep "something" And control-d when finished entering text

You don't need the dash. This is enough: cat | grep "something"

In some older versions of bash, when dealing with process substitution, you actually needed the dash.

For example

    get_data | tee >(cmd)
    get_data | tee >(cat | cmd)
would fail but

    get_data | tee >(cat - | cmd)
would succeed.

Re: Useful use of cat(1)

#23
post #16

Often I find myself dealing with tab-separated files; and cat -n is very handy there. To see which columns a particular value occurs in: awk -F'\t'

FYI you can add line numbers (cat -n behavior) within awk directly as so:

    awk '{print NR, $0}'

Re: Useful use of cat(1)

#24
post #10

ps has a nasty habit of truncating its output. Piping it into cat is a way around this. E.G. $ echo $COLUMNS 118 $ ps -fp 14706 UID PID PPID C STIME TTY TIME CMD root 14706 14705 0 Jan29 pts/3 00:00:00 sudo -p [local sudo] Password: python /usr/lib/sshuttle/main.py pytho $ ps -fp 14706 | cat UID PID PPID C STIME TTY TIME CMD root 14706 14705 0 Jan29 pts/3 00:00:00 sudo -p [local sudo] Password: python /usr/lib/sshutt…

Most generally, it will trick any command into thinking that it's not writing to a tty ; so piping into cat will often remove colors.

Re: Useful use of cat(1)

#25
post #11

Earlier quoted context omitted.

That does not work, but :>foo (the happy file creation operator) does.

What do you mean it doesn't work? ">file" will work on all POSIX shells, it even works on Solaris /bin/sh! Also ":>" is not an operator, it's ":" (a built-in command that does nothing and returns true) and a redirection.

Like the grand-parent, I have definitely used a Unix variant (CX/SX?) which required a command to do redirection.

Re: Useful use of cat(1)

#27
post #11

Earlier quoted context omitted.

That does not work, but :>foo (the happy file creation operator) does.

What do you mean it doesn't work? ">file" will work on all POSIX shells, it even works on Solaris /bin/sh! Also ":>" is not an operator, it's ":" (a built-in command that does nothing and returns true) and a redirection.

It was a joke to call it an operator.

Re: Useful use of cat(1)

#28
post #23
post #16

Often I find myself dealing with tab-separated files; and cat -n is very handy there. To see which columns a particular value occurs in: awk -F'\t'

FYI you can add line numbers (cat -n behavior) within awk directly as so: awk '{print NR, $0}'

The nl command (part of coreutils) is specifically written for numbering lines. It can do simple line numbering, along with various other styles and formats.

Its man page can be seen here: http://linux.die.net/man/1/nl

Re: Useful use of cat(1)

#29
post #28
post #23

Earlier quoted context omitted.

FYI you can add line numbers (cat -n behavior) within awk directly as so: awk '{print NR, $0}'

The nl command (part of coreutils) is specifically written for numbering lines. It can do simple line numbering, along with various other styles and formats. Its man page can be seen here: http://linux.die.net/man/1/nl

awk's sprintf can do all of that. Not saying it's the right tool for the job, but the parent did have a pipeline already using awk.

Re: Useful use of cat(1)

#30
post #13

Earlier quoted context omitted.

| cat > file vs. touch file With 'cat > file' you can actually type in the file's contents (then press ^d to finish). 'touch file' merely creates an empty file. (Not that I think 'cat > file' is much faster than 'vi file')

Ok, I should read more carefully. I thought the article uses cat > file followed by ^D to create empty files. Of course when having some text cat > file is just fine.

No, you can remove 'cat' on most shells:

    > file
Post reply on HN