歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(8)cat命令

每天一個Linux命令(8)cat命令

日期:2017/3/3 13:45:50   编辑:Linux技術

cat命令連接文件並打印到標准輸出設備上,cat經常用來顯示文件的內容,類似於下的type命令。 注意:當文件較大時,文本在屏幕上迅速閃過(滾屏),用戶往往看不清所顯示的內容。因此,一般用more等命令分屏顯示。為了控制滾屏,可以按Ctrl+S鍵,停止滾屏;按Ctrl+Q鍵可以恢復滾屏。按Ctrl+C(中斷)鍵可以終止該命令的執行,並且返回Shell提示符狀態。

[b] (1)用法:[/b] 用法:cat [選項] [文件]...

[b] (2)功能:[/b] 將[文件]或標准輸入組合輸出到標准輸出。

[b] (3)選項參數:[/b] 1)-n, --number 對輸出的所有行編號

2) -s, --squeeze-blank 不輸出多行空行,有連續兩行以上的空白行,就代換為一行的空白行 3) -E, --show-ends 在每行結束處顯示 $

4) -b, --number-nonblank 對非空輸出行編號 5) -A, --show-all 等價於 -vET,顯示不可打印字符,行尾顯示“$”

6) -T, --show-tabs 將跳格字符顯示為 ^I 7) -v, --show-nonprinting 使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外

8) --help 顯示此幫助信息並退出 9) --version 輸出版本信息並退出

[b] (4)實例:[/b] 由於cat命令是查看文檔的,所以首先新建文本文檔test1.txt,test2.txt,test3.txt並在文檔中寫入內容:

方法一:

(1)首先用touch指令新建三個文檔:

[sunjimeng@localhost Document]$ touch {text1.txt,text2.txt,text3.txt}
[sunjimeng@localhost Document]$ ll
總用量 0
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月   4 22:18 text1.txt
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月   4 22:18 text2.txt
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月   4 22:18 text3.txt

(2)用圖形界面,打開文檔輸入數據:

(3)由於在CentOs裡文檔有自動備份的功能,因此這裡有6個文檔。其中帶~符號的需要用查看備份的軟件來打開:

(4)查看shell中的文檔信息:

[sunjimeng@localhost Document]$ ll
總用量 12
-rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:23 text1.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text1.txt~
-rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:23 text2.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text2.txt~
-rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:24 text3.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text3.txt~

方法二:在shell中直接修改文檔的內容:

[sunjimeng@localhost Document]$ touch text4.txt
[sunjimeng@localhost Document]$ cat >text4.txt <<EOF
> test4's first line;
> test4's second line;
> test4's third line;
> EOF
[sunjimeng@localhost Document]$ ll
總用量 16
-rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:23 text1.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text1.txt~
-rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:23 text2.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text2.txt~
-rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:24 text3.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text3.txt~
-rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:31 text4.txt     //這裡並沒有創建備份文件,是區別所在
[sunjimeng@localhost Document]$
1)[sunjimeng@localhost Document]$ cat -n text4.txt 將包括空行在內的各行按編號輸出

[sunjimeng@localhost Document]$ cat >text4.txt <<EOF                   //先修改text4.txt的內容
> text4's first line
> 
> 
> text4's second line
> 
> text4's third line
> 
> 
> EOF
[sunjimeng@localhost Document]$ cat -n text4.txt
     1    text4's first line
     2    
     3    
     4    text4's second line
     5    
     6    text4's third line
     7    
     8

2)[sunjimeng@localhost Document]$ cat -b text4.txt 將除空行在內的各行按編號輸出

[sunjimeng@localhost Document]$ cat -b text4.txt
     1    text4's first line

     2    text4's second line

     3    text4's third line

3)[sunjimeng@localhost Document]$ cat text1.txt text2.txt text3.txt 用cat命令直接輸出各個文件,可以是一個也可以是多個

[sunjimeng@localhost Document]$ cat text1.txt text2.txt 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;
test3's first line;
test3's second line;
test3's third line;

4)[sunjimeng@localhost Document]$ cat text1.txt text2.txt > text5.txt 將講text1.txt和text2.txt輸出到text5.txt裡,和輸出到標准輸出一樣,也可以有-n,-b等參數由於這個特性,cat命令可以將多個壓縮包合並成一個,可以用tar命令解壓

# cat test.tar.gz_?? > test.tar.gz #可以用cat命令將被切割的多個壓縮包合並成一個
# tar -xvzf test.tar.gz #再用tar命令解壓
[sunjimeng@localhost Document]$ cat text1.txt text2.txt > text5.txt
[sunjimeng@localhost Document]$ cat text5.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;
[sunjimeng@localhost Document]$

5)[sunjimeng@localhost Document]$ tac text5.txt 倒序輸出文件的各行內容

[sunjimeng@localhost Document]$ tac text5.txt
test2's third line;
test2's second line;
test2's first line;
test1's third line;
test1's second line;
test1's first line;

6)[sunjimeng@localhost Document]$ cat -s text4.txt 輸出文檔中的內容,如果有多個空行則用一個代替

[sunjimeng@localhost Document]$ cat -s text4.txt                最多連續輸出一個空行
text4's first line

text4's second line

text4's third line

[sunjimeng@localhost Document]$ cat text4.txt                   有多少空行,輸出多少空行
text4's first line

text4's second line

text4's third line

7)[sunjimeng@localhost Document]$ cat >text6.txt 從鍵盤錄入內容到文件,回車是保存,退出Ctrl+z

[sunjimeng@localhost Document]$ cat >text6.txt
I am MenAngel!                                         //除了最後一個回車之後,其余回車是文檔中數據的換行並保存
Practice Order!
^Z          //回車後是Ctrl+Z命令退出
[3]+  已停止               cat > text6.txt
[sunjimeng@localhost Document]$ cat text6.txt
I am MenAngel!
Practice Order!

8)[sunjimeng@localhost Document]$ cat -E text4.txt 輸出各行文本,並且以$符結尾

[sunjimeng@localhost Document]$ cat -E text4.txt
text4's first line$
$
$
text4's second line$
$
text4's third line$
$
$

9)[sunjimeng@localhost Document]$ cat >text6.txt <<EOF 用$取表達式的值小小范例:

[sunjimeng@localhost Document]$ cat >text6.txt <<EOF
> pwd=$(pwd)
> EOF
[sunjimeng@localhost Document]$ cat text6.txt
pwd=/home/sunjimeng/Document

10)[sunjimeng@localhost Document]$ cat --help

[sunjimeng@localhost Document]$ cat --help
用法:cat [選項]... [文件]...
將[文件]或標准輸入組合輸出到標准輸出。

  -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        顯示此幫助信息並退出
      --version        顯示版本信息並退出

如果沒有指定文件,或者文件為"-",則從標准輸入讀取。

示例:
  cat f - g  先輸出f 的內容,然後輸出標准輸入的內容,最後輸出g 的內容。
  cat        將標准輸入的內容復制到標准輸出。

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
請向<http://translationproject.org/team/zh_CN.html> 報告cat 的翻譯錯誤
要獲取完整文檔,請運行:info coreutils 'cat invocation'

11)[sunjimeng@localhost Document]$ cat --version

[sunjimeng@localhost Document]$ cat --version
cat (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
許可證:GPLv3+:GNU 通用公共許可證第3 版或更新版本<http://gnu.org/licenses/gpl.html>。
本軟件是自由軟件:您可以自由修改和重新發布它。
在法律范圍內沒有其他保證。

由Torbjörn Granlund 和Richard M. Stallman 編寫。

Copyright © Linux教程網 All Rights Reserved