1. 程式人生 > 實用技巧 >linux下cat命令詳解

linux下cat命令詳解

cat主要有三大功能:
1.一次顯示整個檔案。$ catfilename
2.從鍵盤建立一個檔案。$ cat>filename
只能建立新檔案,不能編輯已有檔案.
3.將幾個檔案合併為一個檔案: $catfile1file2 > file 引數:
-n 或 --number 由 1 開始對所有輸出的行數編號
-b 或 --number-nonblank 和 -n 相似,只不過對於空白行不編號
-s 或 --squeeze-blank 當遇到有連續兩行以上的空白行,就代換為一行的空白行
-v 或 --show-nonprinting
例:
把 textfile1 的檔案內容加上行號後輸入 textfile2 這個檔案裡
cat -n textfile1 > textfile2
把 textfile1 和 textfile2 的檔案內容加上行號(空白行不加)之後將內容附加到 textfile3 裡。
cat -b textfile1 textfile2 >> textfile3 把
test
.txt檔案扔進垃圾箱,賦空值test.txt
cat /dev/null > /etc/test.txt cat 語法結構;
de>cat [選項] [檔案]...de>
選項
-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 之外

--help 顯示此幫助資訊並離開


1.1 cat 檢視檔案內容例項;

de>[root@localhost ~]# cat /etc/profile注:檢視/etc/目錄下的profile檔案內容;
[root@localhost ~]# cat -b /etc/fstab注:檢視/etc/目錄下的profile內容,並且對非空白行進行編號,行號從1開始;
[root@localhost ~]# cat -n /etc/profile注:對/etc目錄中的profile的所有的行(包括空白行)進行編號輸出顯示;
[root@localhost ~]# cat-E /etc/profile注:檢視/etc/下的profile內容,並且在每行的結尾處附加$符號;de>

cat 加引數-n 和nl工具差不多,檔案內容輸出的同時,都會在每行前面加上行號;

de>[root@localhost ~]# cat -n /etc/profile
[root@localhost ~]# nl/etc/profilede>

cat 可以同時顯示多個檔案的內容,比如我們可以在一個cat命令上同時顯示兩個檔案的內容;

de>[root@localhost ~]# cat /etc/fstab /etc/profilede>

cat 對於內容極大的檔案來說,可以通過管道|傳送到more 工具,然後一頁一頁的檢視;

de>[root@localhost ~]# cat /etc/fstab /etc/profile | morede>


1.2 cat 的建立、連線檔案功能例項;


cat 有建立檔案的功能,建立檔案後,要以EOF或STOP結束;

de>[root@localhost ~]# cat >linuxsir.org.txt<< EOF注:建立linuxsir.org.txt檔案;
> 我來測試 cat 建立檔案,並且為檔案輸入內容;注:這是為linuxsir.org.txt檔案輸入內容;
> 北南南北 測試;注:這是為linuxsir.org.txt檔案輸入內容;
> EOF注:退出編輯狀態;

[root@localhost ~]# cat linuxsir.org.txt注:我們檢視一下linuxsir.org.txt檔案的內容;

我來測試 cat 建立檔案,並且為檔案輸入內容;
北南南北 測試;de>

cat 還有向已存在的檔案追加內容的功能;

de>[root@localhost ~]# catlinuxsir.txt 注:檢視已存在的檔案linuxsir.txt 內容;
I am BeiNanNanBei From LinuxSir.Org .注:內容行
我正在為cat命令寫文件


[root@localhost ~]# cat >> linuxsir.txt << EOF注:我們向linuxsir.txt檔案追加內容;
> 我來測試cat向文件追加內容的功能;注:這是追回的內容
> OK?
> OK~
> 北南 呈上
> EOF注:以EOF退出;


[root@localhost ~]# cat linuxsir.txt注:檢視檔案內容,看是否追回成功。
I am BeiNanNanBei From LinuxSir.Org .
我正在為cat命令寫文件

我來測試cat向文件追加內容的功能;
OK?
OK~
北南 呈上de>




cat 連線多個檔案的內容並且輸出到一個新檔案中;

假設我們有sir01.txt、sir02.tx和sir03.txt ,並且內容如下;

de>[root@localhost ~]# cat sir01.txt
123456
i am testing

[root@localhost ~]# cat sir02.txt
56789
BeiNan Tested

[root@localhost ~]# cat sir03.txt
09876
linuxsir.org testingde>

我想通過cat 把sir01.txt、sir02.txt及sir03.txt 三個檔案連線在一起(也就是說把這三個檔案的內容都接在一起)並輸出到一個新的檔案sir04.txt 中。

注意:其原理是把三個檔案的內容連線起來,然後建立sir04.txt檔案,並且把幾個檔案的內容同時寫入sir04.txt中。特別值得一提的是,如果您輸入到一個已經存在的sir04.txt 檔案,會把sir04.txt內容清空。

de>[root@localhost ~]# cat sir01.txt sir02.txt sir03.txt > sir04.txt

[root@localhost ~]# more sir04.txt
123456
i am testing
56789
BeiNan Tested
09876
linuxsir.org testingde>

cat 把一個或多個已存在的檔案內容,追加到一個已存在的檔案中

de>[root@localhost ~]# cat sir00.txt
linuxsir.org forever

[root@localhost ~]# cat sir01.txt sir02.txt sir03.txt >> sir00.txt

[root@localhost ~]# cat sir00.txt
linuxsir.org forever
123456
i am testing
56789
BeiNan Tested
09876
linuxsir.org testingde>

警告:我們要知道>意思是建立,>>是追加。千萬不要弄混了。造成失誤可不是鬧著玩的;