Useful use of cat(1)
in-ulm.de
in-ulm.de
$ 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 alias new='ls -lthr --color=always | tail'
Normally ls will not use colours when writing to a pipe (not a tty).I've always preferred less - lets you scroll backwards & forwards through the piped output.
cat intermediate1.mpg intermediate2.mpg > intermediate_all.mpg http://ffmpeg.org/faq.html#How-can-I-join-video-files_003f
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. :)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 file2I almost resisted the urge, but no, the pedant in me :-) insists on pointing out that cat is short for catenate
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 catWith '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')
Of course when having some text cat > file is just fine.
> fileI 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 network are the bottleneck not CPU speed or memory bandwidth. Also, when working on the command line you can drop in useful tools like pv in place of cat.
<file cmd 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.
>foo
also works to create file "foo". :>foo
(the happy file creation operator) does.Also ":>" is not an operator, it's ":" (a built-in command that does nothing and returns true) and a redirection.
echo -n > fileTo see which columns a particular value occurs in:
awk -F'\t' < tsv-file | tr '\t' '\n' | cat -n | grep something awk '{print NR, $0}'Its man page can be seen here: http://linux.die.net/man/1/nl
cat - | grep "something"
And control-d when finished entering text cat | grep "something" grep "something"For example
get_data | tee >(cmd)
get_data | tee >(cat | cmd)
would fail but get_data | tee >(cat - | cmd)
would succeed.