系統(tǒng)之家 - 系統(tǒng)光盤下載網(wǎng)站!

當(dāng)前位置:系統(tǒng)之家 > 系統(tǒng)教程 > Linux使用cat命令

Linux下如何使用cat命令?

時(shí)間:2014-12-09 14:11:00 作者:qipeng 來源:系統(tǒng)之家 1. 掃描二維碼隨時(shí)看資訊 2. 請使用手機(jī)瀏覽器訪問: https://m.xitongzhijia.net/xtjc/20141208/32129.html 手機(jī)查看 評論

  Linux系統(tǒng)下如果要同時(shí)顯示多個(gè)文件的內(nèi)容,就要使用cat命令了,那么cat命令還有那些用途呢?下面隨小編一起來了解下Linux下cat命令的用法。

 Linux下如何使用cat命令?

  cat命令的用途是連接文件或標(biāo)準(zhǔn)輸入并打印。這個(gè)命令常用來顯示文件內(nèi)容,或者將幾個(gè)文件連接起來顯示,或者從標(biāo)準(zhǔn)輸入讀取內(nèi)容并顯示,它常與重定向符號配合使用。

  1.命令格式:

  cat [選項(xiàng)] [文件]。。。

  2.命令功能:

  cat主要有三大功能:

  1.一次顯示整個(gè)文件:cat filename

  2.從鍵盤創(chuàng)建一個(gè)文件:cat 》 filename 只能創(chuàng)建新文件,不能編輯已有文件。

  3.將幾個(gè)文件合并為一個(gè)文件:cat file1 file2 》 file

  3.命令參數(shù):

  -A, --show-all 等價(jià)于 -vET

  -b, --number-nonblank 對非空輸出行編號

  -e 等價(jià)于 -vE

  -E, --show-ends 在每行結(jié)束處顯示 $

  -n, --number 對輸出的所有行編號,由1開始對所有輸出的行數(shù)編號

  -s, --squeeze-blank 有連續(xù)兩行以上的空白行,就代換為一行的空白行

  -t 與 -vT 等價(jià)

  -T, --show-tabs 將跳格字符顯示為 ^I

  -u (被忽略)

  -v, --show-nonprinting 使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外

  4.使用實(shí)例:

  實(shí)例一:把 log2012.log 的文件內(nèi)容加上行號后輸入 log2013.log 這個(gè)文件里

  命令:

  cat -n log2012.log log2013.log

  輸出:

 。踨oot@localhost test]# cat log2012.log

  2012-01

  2012-02

  ======[root@localhost test]# cat log2013.log

  2013-01

  2013-02

  2013-03

  ======[root@localhost test]# cat -n log2012.log log2013.log

  1 2012-01

  2 2012-02

  3

  4

  5 ======

  6 2013-01

  7 2013-02

  8

  9

  10 2013-03

  11 ======[root@localhost test]#

  說明:

  實(shí)例二:把 log2012.log 和 log2013.log 的文件內(nèi)容加上行號(空白行不加)之后將內(nèi)容附加到 log.log 里。

  命令:

  cat -b log2012.log log2013.log log.log

  輸出:

  [root@localhost test]# cat -b log2012.log log2013.log log.log

  1 2012-01

  2 2012-02

  3 ======

  4 2013-01

  5 2013-02

  6 2013-03

  7 ======[root@localhost test]#

  實(shí)例三:把 log2012.log 的文件內(nèi)容加上行號后輸入 log.log 這個(gè)文件里

  命令:

  輸出:

 。踨oot@localhost test]# cat log.log

 。踨oot@localhost test]# cat -n log2012.log 》 log.log

 。踨oot@localhost test]# cat -n log.log

  1 2012-01

  2 2012-02

  3

  4

  5 ======

  [root@localhost test]#

  實(shí)例四:使用here doc來生成文件

  輸出:

 。踨oot@localhost test]# cat 》log.txt 《《EOF

  》 Hello

  》 World

  》 Linux

  》 PWD=$(pwd)

  》 EOF

 。踨oot@localhost test]# ls -l log.txt

  -rw-r--r-- 1 root root 37 10-28 17:07 log.txt

 。踨oot@localhost test]# cat log.txt

  Hello

  World

  Linux

  PWD=/opt/soft/test

 。踨oot@localhost test]#

  說明:

  注意粗體部分,here doc可以進(jìn)行字符串替換。

  備注:

  tac (反向列示)

  命令:

  tac log.txt

  輸出:

 。踨oot@localhost test]# tac log.txt

  PWD=/opt/soft/test

  Linux

  World

  Hello

  說明:

  tac 是將 cat 反寫過來,所以他的功能就跟 cat 相反, cat 是由第一行到最后一行連續(xù)顯示在螢?zāi)簧,?tac 則是由最后一行到第一行反向在螢?zāi)簧巷@示出來!

  上面就是Linux下cat命令的用法介紹了,使用cat命令來讀取短文件是非常方便的,使用組合命令能夠達(dá)到出其不意的效果。

標(biāo)簽 命令

發(fā)表評論

0

沒有更多評論了

評論就這些咯,讓大家也知道你的獨(dú)特見解

立即評論

以上留言僅代表用戶個(gè)人觀點(diǎn),不代表系統(tǒng)之家立場

其他版本軟件

熱門教程

人氣教程排行

Linux系統(tǒng)推薦

官方交流群 軟件收錄