Live data from Hacker News

Useful use of cat(1)

in-ulm.de

31–40 of 46 posts

Re: Useful use of cat(1)

#31
post #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.

Of course if you want colours, you can often force programs to use them anyway. For example, I use the alias:

  alias new='ls -lthr --color=always | tail'
Normally ls will not use colours when writing to a pipe (not a tty).

Re: Useful use of cat(1)

#32
post #18

Don't forget "tac" - the reverse of "cat".

No, that just concatenates a file reading bytes from the end, backwards. The real opposite of cat would be a putative 'trunc'. So if you typed $ cat somefile It would print the file to your terminal. And then if you said $ trunc somefile The printed lines would be removed from your terminal. :)

In case you missed it, "tac" is literally the reverse of "cat". As in "cat".reverse() or something.

Re: Useful use of cat(1)

#34
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"

When I was young, I used cat and grep like that until an old Unix sys-admin remarked "What a gratuitous use of cat!". At the time, I did not understand what he meant, but now I do. So whenever I see cat and grep used like this, I think back to that lesson I learned.

Re: Useful use of cat(1)

#35
post #2

You may also create new empty files by saying `touch filename`. This saves three keystrokes (which includes an annoying `>` symbol, requiring the shift key) as compared to the aforementioned `cat` method!

You can also use 'echo "hello" > file'

technically the equivalent is

    echo -n > file

Re: Useful use of cat(1)

#36
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'

Is there a version of awk that has interesting default behavior when you leave out the program like that?

Re: Useful use of cat(1)

#37
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…

"... Piping it into cat is a way around this. ..."

I've always preferred less - lets you scroll backwards & forwards through the piped output.

Re: Useful use of cat(1)

#39
post #13

Earlier quoted context omitted.

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

What shell? I'm running bash 4.2.20(2)-release, and "> file" is behaving like "touch file".
Post reply on HN