linux下cat命令詳解

簡略版:

cat主要有三大功能:
1.一次顯示整個文件。$ cat filename
2.從鍵盤創建一個文件。$ cat > filename  
   只能創建新文件,不能編輯已有文件.
3.將幾個文件合併爲一個文件: $cat file1 file2 > 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  

 

 

 

詳細點的:

轉:http://blog.sina.com.cn/s/blog_52f6ead0010127xm.html

cat 是一個文本文件查看和連接工具。查看一個文件的內容,用cat比較簡單,就是cat 後面直接接文件名。

比如:

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

爲了便於新手弟兄靈活掌握這個工具,我們多說一點常用的參數;


1.0 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 ~]# cat  linuxsir.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>

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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章