People seem to forget the true usefulness of the cat command.
Okay, stop laughing.
Just use cat to, well, cat a file to standard output or to another file.
mek@lap1:~/stats/baseball-stats> cat tables.txt Allstar AwardsManagers AwardsPlayers AwardsShareManagers AwardsSharePlayers
Others might use it instead of an editor for short files.
mek@lap1:~/stats/baseball-stats> cat > INFO This directory contains all the data files for create my baseball database ^D mek@lap1:~/stats/baseball-stats> cat INFO This directory contains all the data files for create my baseball database
You might also use it to turn multiple files into a single file.
mek@lap1:~/stats/baseball-stats> cat myheader INFO > INFO.txt mek@lap1:~/stats/baseball-stats> cat INFO.txt ### These files were created by Mat Kovach <mek@mek.cc> ### This directory contains all the data files for create my baseball database
But cat can be much more useful. Just taking a look at the man page shows a number of options, including:
- -b: number non-blank lines in output
- -n: number ALL lines in output
- -s: display never more than one blank line in output
- -E: display '$' at the end of each line in output
- -v: show non-printing charactors in output
- -e: same as -vE
- -T: show tabs at ^I
If we take the following file:
mek@lap1:~/stats/baseball-stats> cat db-setup.R
library(DBI)
library(RMySQL)
drv<-dbDriver('MySQL')
con<-dbConnect(drv,username="baseball",password="baseball",dbname="baseball",host="localhost")
query<-dbSendQuery(con,"select * from Allstar")
allstar<- fetch(query,n=-1)
query<-dbSendQuery(con,"select * from AwardsManagers")
awardsmanagers<- fetch(query,n=-1)
We can see if there is anything "hidden" in it.
mek@lap1:~/stats/baseball-stats> cat -eTsn db-setup.R
1 library(DBI)$
2 library(RMySQL)$
3 $
4 drv<-dbDriver('MySQL')$
5 con<-dbConnect(drv,username="baseball",password="baseball",dbname="baseball",host="localhost")$
6 $
7 query<-dbSendQuery(con,"select * from Allstar")$
8 allstar<- fetch(query,n=-1) $
9 $
10 query<-dbSendQuery(con,"select * from AwardsManagers")^I$
11 awardsmanagers<- fetch(query,n=-1)$
12 $Notice the extra blank lines have be turned into a single blank line. You'll aso see that line 8 has extra spaces at the end of it and line 10 has a tab at the end of it. Some applications may generate an error in those cases.
If you program and use make, you might have seen this error:
mek@lap1:~/source/football-stats> make create.sh Makefile:13: *** missing separator (did you mean TAB instead of 8 spaces?). Stop.
Using cat:
mek@lap1:~/source/football-stats> cat -neT Makefile ... 12 create.sh: src/conf-dbname src/conf-table-sql src/print-create.sh src/warn-autogen.txt $ 13 @rm -f create.sh$ 14 ^I @(cd src/; \$ 15 /bin/sh warn-autogen.txt; /bin/sh print-create.sh) >create.sh$ 16 ^I@chmod 555 create.sh$ 17 ^I$
At line 13 it is visible that the tab is missing.
You can also have cat output to standard output but using a '-' as the only filename. This can be useful when using ssh to transfer files.
mek@lap1:~> cat .ssh/id_rsa.pub | ssh kovachme@irc.nooss.org 'cat - >> ~/.ssh/authorized_keys' kovachme@irc.nooss.org's password: mek@lap1:~>
