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/桌面

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM