
Useful use of cat(1) - gnosis
http://www.in-ulm.de/~mascheck/various/uuoc/
======
sciurus
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/sshuttle/main.py python -v --firewall 12300 0

~~~
emillon
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.

~~~
burgerbrain
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).

------
emillon
A very nice (but a bit academic) way to look at "cat" as a monadic "return" on
unix pipes :

<http://okmij.org/ftp/Computation/monadic-shell.html>

------
martron
Also surprisingly useful in command line video editing. For instance, you can
join mpg files together...

cat intermediate1.mpg intermediate2.mpg > intermediate_all.mpg
<http://ffmpeg.org/faq.html#How-can-I-join-video-files_003f>

~~~
icebraining
Well, that's the main purpose of cat - con _cat_ enate files and print on the
standard output.

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

~~~
neilk
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. :)

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

~~~
sciurus
I get that tac exists and is useful for concatenating and printing files in
reverse.

Neilk was just humorously pointing out that since cat stands for concatenate,
its reverse should be truncate, and since cat is often abused to print a
single file to a terminal this fictional trunc should "unprint" it.

Arguably you could view 'comm -3' as the reverse of cat.

    
    
      # output is all the lines of file1 and file2
      cat file1 file2
    
      # output is the lines in file1 not in file 2, and vice verse
      comm -3 file1 file2

~~~
emmelaich
> cat stands for concatenate

I almost resisted the urge, but no, the pedant in me :-) insists on pointing
out that cat is short for _cat_ enate

------
badboy

      cat > file vs. touch file
      cat file | cmd vs. cmd < file
      cmd1 | cmd2 | cat vs. { cmd1 | cmd2 || true; } vs. cmd1 | cmd2; true
    

So even some of the thing listed are still not the best use case for _cat_

~~~
cristoperb
| 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')

~~~
badboy
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.

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

    
    
        > file

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

~~~
icebraining
You're right, I was mistaken. It's just in Z shell.

~~~
salvadors
You mean there are other shells?

------
dredmorbius
The "convert file into arguments" trick is useful, but a "while read/do" loop
would be better:

    
    
        cat file | while read INPUT
        do
            command "$INPUT"
        done
    

... which will read the _entire_ line from 'file' into the shell variable
"$INPUT", inclusive of whitespace. If you need to do further parsing, of
course, you can do that within the loop.

Trap: since you're creating a subshell (at least in bash/Bourne), variables
defined within the loop are _NOT_ defined after it exits.

------
ajays
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' < tsv-file | tr '\t' '\n' | cat -n | grep something

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

    
    
        awk '{print NR, $0}'

~~~
gnosis
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>

~~~
veyron
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.

------
memset
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!

~~~
Ives
Or, at least when running Bash (possibly sh, didn't try), just

    
    
       >foo
    

also works to create file "foo".

~~~
koenigdavidmj
That does not work, but

    
    
      :>foo
    

(the happy file creation operator) does.

~~~
avar
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.

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

------
mhitza
Instead of using a cat with heredoc, just type

    
    
        cat - | grep "something"
    

And control-d when finished entering text

~~~
gnosis
You don't need the dash. This is enough:

    
    
      cat | grep "something"

~~~
comex
Or just

    
    
        grep "something"

------
mdesantis
I thought it was a post about cats >.<

