linux 下cat的詳細用法

來源:互聯網
上載者:User

標籤:cat

cat命令的用途是串連檔案或標準輸入並列印。這個命令常用來顯示檔案內容,或者將幾個檔案串連起來顯示,或者從標準輸入讀取內容並顯示,它常與重新導向符號配合使用。

1.命令格式:

cat [選項] [檔案]...

2.命令功能:

cat主要有三大功能:

1.一次顯示整個檔案:cat filename

2.從鍵盤建立一個檔案:cat > filename 只能建立新檔案,不能編輯已有檔案.

3.將幾個檔案合并為一個檔案:cat file1 file2 > file

3.命令參數:

-A, --show-all           等價於 -vET
-b, --number-nonblank    對非空輸出行編號
-e                       等價於 -vE
-E, --show-ends          在每行結束處顯示 $
-n, --number     對輸出的所有行編號,由1開始對所有輸出的行數編號
-s, --squeeze-blank  有連續兩行以上的空白行,就代換為一行的空白行 
-t                       與 -vT 等價
-T, --show-tabs          將跳格字元顯示為 ^I
-u                       (被忽略)
-v, --show-nonprinting   使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外

4.使用執行個體:

執行個體一:把 log2012.log 的檔案內容加上行號後輸入 log2013.log 這個檔案裡

命令:

cat -n log2012.log log2013.log

輸出:


複製代碼

代碼如下:


[[email protected] test]# cat log2012.log 
2012-01
2012-02</p> <p>======[[email protected] test]# cat log2013.log 
2013-01
2013-02</p> <p>2013-03
======[[email protected] 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 ======[[email protected] test]#

說明:


執行個體二:把 log2012.log 和 log2013.log 的檔案內容加上行號(空白行不加)之後將內容附加到 log.log 裡。

命令:

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

輸出:


複製代碼

代碼如下:


[[email protected] 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 ======[[email protected] test]#

執行個體三:把 log2012.log 的檔案內容加上行號後輸入 log.log 這個檔案裡

命令:

輸出:


複製代碼

代碼如下:


[[email protected] test]# cat log.log 
[[email protected] test]# cat -n log2012.log > log.log
[[email protected] test]# cat -n log.log 
1 2012-01
2 2012-02
3
4
5 ======
[[email protected] test]#

執行個體四:使用here doc來組建檔案

輸出:


複製代碼

代碼如下:


[[email protected] test]# cat >log.txt <<EOF
> Hello
> World
> Linux
> PWD=$(pwd)
> EOF
[[email protected] test]# ls -l log.txt 
-rw-r--r-- 1 root root 37 10-28 17:07 log.txt
[[email protected] test]# cat log.txt 
Hello
World
Linux
PWD=/opt/soft/test
[[email protected] test]#

說明:

注意粗體部分,here doc可以進行字串替換。


備忘:

tac (反向列示)

命令:

tac log.txt

輸出:


複製代碼

代碼如下:


[[email protected] test]# tac log.txt 
PWD=/opt/soft/test
Linux
World
Hello

tac 是將 cat 反寫過來,所以他的功能就跟 cat 相反, cat 是由第一行到最後一行連續顯示在螢幕上,而 tac 則是由最後一行到第一行反向在螢幕上顯示出來!


本文出自 “gaogaozi” 部落格,請務必保留此出處http://hangtiangazi.blog.51cto.com/8584103/1622728

linux 下cat的詳細用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.