Linux命令之cat

cat [选项] [文件]

将文件或标准输入组合输出到标准输出

如果没有指定文件,或文件为’-’,则从标准输入读取

注意:当文件较大时,文本在屏幕上迅速闪过(滚屏),用户往往看不清显示的内容。因此,一般用more等命令分屏显示。为了控制滚屏,可以按Ctrl+S停止滚屏;Ctrl+QQ回复滚屏;Ctrl+C终止该命令,并回到Shell提示符状态。

(1).选项
-A,--show-all 等于-vET

-b,--number-nonblank 对非空输出行编号

-e 等于-vE

-E,--show-ends 在每行结束处显示”$”

-n,--number 对输出的所有行编号

-s,--squeeze-blank 不输出多行空行

-t 与-vT等价

-T,--show-tabs 将跳格字符显示为^I

-u (被忽略)

-v,--show-nonprinting 使用^和M-引用,除了LFD和TAB之外

(2).实例

 图形界面中修改文档和Shell中修改文档

[root@CentOS6 桌面]# touch text1.txt
[root@CentOS6 桌面]# ll
总用量 0
-rw-r--r--. 1 root root 0 6月  24 00:24 text1.txt
[root@CentOS6 桌面]# ll    //图形界面修改文档
总用量 4
-rw-r--r--. 1 root root 61 6月  24 00:25 text1.txt
-rw-r--r--. 1 root root  0 6月  24 00:24 text1.txt~    //可以看到多来一个备份文档
[root@CentOS6 桌面]# touch text2.txt
[root@CentOS6 桌面]# cat>text2.txt <<EOF
> test2's first line;
> test2's second line;
> test2's third line;
> EOF
[root@CentOS6 桌面]# ll
总用量 8
-rw-r--r--. 1 root root 61 6月  24 00:25 text1.txt
-rw-r--r--. 1 root root  0 6月  24 00:24 text1.txt~
-rw-r--r--. 1 root root 61 6月  24 00:26 text2.txt    //并没有备份文档

 cat修改文档,会重现编写整个文档

[root@CentOS6 桌面]# cat>text2.txt<<EOF
> text2's first line
> EOF
[root@CentOS6 桌面]# cat text2.txt 
text2's first line

将包括空行在内的所有行编号

[root@CentOS6 桌面]# cat>text2.txt<<EOF 
> test2's first line
> 
> test2's second line
> 
> 
> test2's third line
> EOF
[root@CentOS6 桌面]# cat -n text2.txt 
     1	test2's first line
     2	
     3	test2's second line
     4	
     5	
     6	test2's third line

 将除空行外所有行编号

[root@CentOS6 桌面]# cat -b text2.txt 
     1	test2's first line

     2	test2's second line


     3	test2's third line

 用cat直接输出文件,可以是一个也可以是多个

[root@CentOS6 桌面]# cat text1.txt text2.txt 
test1's first line;
test1's second line;
test1's third line;
test2's first line

test2's second line


test2's third line

 将text1.txt和text2.txt输出到text3.txt中,和输出到标准输出一样,可以有选项参数。由于这个特性cat可以将多个压缩包压缩成一个,可以用tar命令解压

[root@CentOS6 桌面]# cat text1.txt text2.txt >text3.txt
[root@CentOS6 桌面]# cat text3.txt 
test1's first line;
test1's second line;
test1's third line;
test2's first line

test2's second line


test2's third line

倒序输出文档内容

[root@CentOS6 桌面]# tac text3.txt     //这个命令真是吓到我了,cat倒过来写就是倒序输出吗
test2's third line


test2's second line

test2's first line
test1's third line;
test1's second line;
test1's first line;

 最多输出一个空行

[root@CentOS6 桌面]# cat -s text2.txt 
test2's first line

test2's second line

test2's third line

 除了cat>text4.txt<<EOF外,还可以使用cat>text4.txt录入内容,Ctrl+Z退出

[root@CentOS6 桌面]# cat>text4.txt
I am MenAngel!
Practice Order!
^Z
[1]+  Stopped                 cat > text4.txt
[root@CentOS6 桌面]# cat text4.txt 
I am MenAngel!
Practice Order!

 输出各行以$符号结尾

[root@CentOS6 桌面]# cat -E text2.txt 
test2's first line$
$
test2's second line$
$
$
test2's third line$

 文档中使用$取表达式的值

[root@CentOS6 桌面]# cat >text5.txt<<EOF
> pwd=$(pwd)
> EOF
[root@CentOS6 桌面]# cat text5.txt 
pwd=/root/桌面
原文地址:https://www.cnblogs.com/diantong/p/9243440.html