Linux書籤(08)用linux cat新建/查看/拷貝/合併文件與輸出打印

楔子:作爲一名經常開車的老司機,查看日誌文件是一種家常便飯。Linux more 命令就是這樣一道非常下飯的菜,學會用它查看日誌文件,也許會讓很多問題及時被發現和定位。

Linux cat 命令

 

Linux cat 命令,用於連接文件並打印到標準輸出設備上。與 tac 相反,cat 是從第一行到最後一行連續顯示在螢幕上。

 

語法格式

cat [-選參] [--help] [--version] fileName

選參說明

可選參數簡寫 可選參數全稱 含義
-n --number 由 1 開始對所有輸出的行數編號
-b --number-nonblank 和 -n 相似,只不過對於空白行不編號
-s --squeeze-blank 當遇到有連續兩行以上的空白行,就代換爲一行的空白行
-v --show-nonprinting 使用 ^ 和 M- 符號,除了 LFD 和 TAB 之外
-E --show-ends 在每行結束處顯示 $
-T --show-tabs 將 TAB 字符顯示爲 ^I
-A --show-all 等價於 -vET 選項
-e   等價於 -vE 選項
-t   等價於 -vT 選項
--help   顯示幫助並退出
--version   顯示版本信息並退出,沒有指定文件或指定的文件是 -,則從標準輸入讀取

 

參考示例

01)創建新文件

[devuser@sh-k82 ~]$ cat >  test.txt << EOF
> hello cat
> cat cat cat
> it is not cat 
> 
> test
> 
> EOF

02)查看文件的內容

[devuser@sh-k82 ~]$ cat test.txt 
hello cat
cat cat cat
it is not cat 

test

03)查看文件的內容,並顯示行數編號

[devuser@sh-k82 ~]$ cat -n test.txt 
     1	hello cat
     2	cat cat cat
     3	it is not cat 
     4	
     5	test
     6	

04)查看文件的內容,並添加行數編號後輸出到另外一個文件中

[devuser@sh-k82 ~]$ cat -n test.txt > test2.txt
[devuser@sh-k82 ~]$ ll
total 8
-rw-rw-r-- 1 devuser devuser 98 Oct  9 22:41 test2.txt
-rw-rw-r-- 1 devuser devuser 49 Oct  9 22:32 test.txt
[devuser@sh-k82 ~]$ cat -n test2.txt 
     1	     1	hello cat
     2	     2	cat cat cat
     3	     3	
     4	     4	it is not that cat
     5	     5	
     6	     6	test
     7	     7	

05)複製文件

[devuser@sh-k82 ~]$ cat test.txt > test3.txt
[devuser@sh-k82 ~]$ ll
total 12
-rw-rw-r-- 1 devuser devuser 98 Oct  9 22:41 test2.txt
-rw-rw-r-- 1 devuser devuser 49 Oct  9 22:42 test3.txt
-rw-rw-r-- 1 devuser devuser 49 Oct  9 22:32 test.txt
[devuser@sh-k82 ~]$ cat -n test3.txt 
     1	hello cat
     2	cat cat cat
     3	
     4	it is not that cat
     5	
     6	test
     7	
[devuser@sh-k82 ~]$ 

06)清空文件內容

[devuser@sh-k82 ~]$ cat /dev/null test.txt > test.txt 
cat: test.txt: input file is output file
[devuser@sh-k82 ~]$ cat test.txt 
[devuser@sh-k82 ~]$ 

注:dev/null:在類 Unix 系統中,/dev/null 稱空設備,是一個特殊的設備文件,它丟棄一切寫入其中的數據(但報告寫入操作成功),讀取它則會立即得到一個 EOF。

而使用 cat $filename > /dev/null 則不會得到任何信息,因爲我們將本來該通過標準輸出顯示的文件信息重定向到了 /dev/null 中。

使用 cat $filename 1 > /dev/null 也會得到同樣的效果,因爲默認重定向的 1 就是標準輸出。如果你對 shell 腳本或者重定向比較熟悉的話,應該會聯想到 2,也即標準錯誤輸出。

如果我們不想看到錯誤輸出呢?我們可以禁止標準錯誤 cat $badname 2 > /dev/null。

07)持續寫入文件內容,碰到 END 符後自動結束並保存文件

[devuser@sh-k82 ~]$ cat > test.txt << END
> hello cat
> 
> 
> cat cat cat
> 
> 
> it is not that cat
> 
> test
> END
[devuser@sh-k82 ~]$ cat test.txt 
hello cat


cat cat cat


it is not that cat

test
[devuser@sh-k82 ~]$ 

08)對非空行進行編號,空行不編號進行輸出打印

[devuser@sh-k82 ~]$ cat -b test.txt 
     1	hello cat


     2	cat cat cat


     3	it is not that cat

     4	test
[devuser@sh-k82 ~]$ 

09)有時候文件中空行會很多,需要將多個空行合併爲一行進行輸出(不會改變文件內容)

[devuser@sh-k82 ~]$ cat -s test.txt 
hello cat

cat cat cat

it is not that cat

test
[devuser@sh-k82 ~]$ 

10)在每行結束處顯示 $

[devuser@sh-k82 ~]$ cat -E test.txt 
hello cat$
$
$
cat cat cat$
$
$
it is not that cat$
$
test$
[devuser@sh-k82 ~]$ 

11)合併多個文件內容到一個新文件

[devuser@sh-k82 ~]$ cat test2.txt test3.txt > test4.txt
[devuser@sh-k82 ~]$ ll
total 16
-rw-rw-r-- 1 devuser devuser  98 Oct  9 22:41 test2.txt
-rw-rw-r-- 1 devuser devuser  49 Oct  9 22:42 test3.txt
-rw-rw-r-- 1 devuser devuser 147 Oct  9 23:10 test4.txt
-rw-rw-r-- 1 devuser devuser  51 Oct  9 22:50 test.txt
[devuser@sh-k82 ~]$ cat test4.txt 
     1	hello cat
     2	cat cat cat
     3	
     4	it is not that cat
     5	
     6	test
     7	
hello cat
cat cat cat

it is not that cat

test

[devuser@sh-k82 ~]$ 

12)製作鏡像文件。例如要製作軟盤的鏡像文件,將軟盤放好後輸入:

cat /dev/fd0 > OUTFILE

注:OUTFILE 指輸出的鏡像文件名

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