【Linux常用命令】 cat

Usage: cat [OPTION] [FILE]...
Concatenate FILE(s), or standard input, to standard output. 连接文件或标准输入,显示到标准输出

-A, --show-all equivalent to -vET  显示所有,等同于-vET
-b, --number-nonblank number nonblank output lines  对非空行进行编号显示
-e equivalent to -vE  等同于-vE
-E, --show-ends display $ at end of each line  在每行的结尾处显示$
-n, --number number all output lines  显示所有输出行的行号(包含空行)
-s, --squeeze-blank never more than one single blank line   压缩空行,连续的多个空行压缩成一个显示
-t equivalent to -vT  等同于-vT
-T, --show-tabs display TAB characters as ^I  显示TAB为字符^I
-u (ignored)  (忽略)
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB   显示非打印字符,使用^和M-表示,除了LFD和TAB
--help display this help and exit  显示帮助和退出
--version output version information and exit 输出版本信息和退出

With no FILE, or when FILE is -, read standard input. 如果没有FILE,或当FILE是-时,读取标准输入

Examples:
cat f - g Output f's contents, then standard input, then g's contents.  输出:文件f的内容,然后标准输入,然后文件g的内容。
cat Copy standard input to standard output.  复制标准输入到标准输出。

Report bugs to <bug-coreutils@gnu.org>.

-------------------------------------------------------------------------------------------------------------------

cat主要用途:

1. 显示文件内容到标准输出。

cat time_test.c

 1 [loong@localhost ~]$ cat time_test.c 
2 #include <stdio.h>
3 #include <time.h>
4
5
6 int main() {
7 time_t biggest = 0x7FFFFFFF;
8
9
10
11 printf("biggest = %s \n", asctime(gmtime(&biggest)));
12 return 0;
13 }

其中可以添加OPTIONS,来添加要显示的内容,如行号、非可见字符(TAB),或每行结尾处显示$等。参见上面的USAGE。

2. 创建新文件,并输入文件内容。(注:只能顺序输入) ps:只能创建新文件,不能编译已有文件。

cat > test.txt

其实是利用>重定向标准输出到文件test.txt。

 1 [loong@localhost ~]$ cat > test.txt
2 this is a test for cat command.^[[D
3 a
4 b
5 cc
6 de
7 [loong@localhost ~]$ cat test.txt
8 this is a test for cat command.
9 a
10 b
11 cc
12 de

 

 其中^[[D是向左的箭头,本想回退一格编辑,但很可惜只能顺序输入。如果你command拼写错误,如多写了一个m,则只能删除到错误出,重新输入后面的内容。

另外,如何结束输入呢???

在linux下:

ctrl+d:代表输入完成或者注销
ctrl+c:中断当前程序
ctrl+z:将作业放置到后台(暂停)

所以,此处应使用CTRL+D,结束输入。

3. 将几个文件合并成一个文件。

cat b.txt a.txt > c.txt

1 [loong@localhost ~]$ cat > a.txt
2 this is file 1.
3 [loong@localhost ~]$ cat > b.txt
4 this is file 2.[loong@localhost ~]$ cat a.txt
5 this is file 1.
6 [loong@localhost ~]$ cat b.txt
7 this is file 2.[loong@localhost ~]$ cat b.txt a.txt > c.txt
8 [loong@localhost ~]$ cat c.txt
9 this is file 2.this is file 1.

注:文件b.txt少输入了一个回车ENTER,所以文件c.txt输出为一行。

另外可以添加一些OPTIONS使用,如下:

$ cat -n a.txt c.txt > d.txt 将a.txt和c.txt加上行号,合并到d.txt中。 

1 [loong@localhost ~]$ cat -n a.txt c.txt > d.txt
2 [loong@localhost ~]$ cat d.txt
3 1 this is file 1.
4 2 this is file 2.this is file 1. 

 

参考:http://linux.chinaunix.net/techdoc/system/2007/11/16/972467.shtml

posted on 2012-03-31 11:09  Lo0ong  阅读(1589)  评论(0编辑  收藏  举报

导航