每天(tian)一個linux命(ming)令(10):cat 命(ming)令
cat命(ming)令的(de)用(yong)(yong)途(tu)是(shi)連接文(wen)件或(huo)標準(zhun)輸入(ru)并打印。這個命(ming)令常用(yong)(yong)來顯示(shi)文(wen)件內容(rong),或(huo)者將(jiang)幾(ji)個文(wen)件連接起(qi)來顯示(shi),或(huo)者從標準(zhun)輸入(ru)讀取內容(rong)并顯示(shi),它常與重定向符號配合使用(yong)(yong)。
1.命令(ling)格(ge)式:
cat [選項] [文(wen)件]...
2.命令功(gong)能:
cat主要有三大(da)功能:
1.一次顯示(shi)整個(ge)文件:cat filename
2.從(cong)鍵盤(pan)創(chuang)建一個文件(jian)(jian)(jian):cat > filename 只能創(chuang)建新文件(jian)(jian)(jian),不能編輯已有文件(jian)(jian)(jian).
3.將幾個(ge)文件(jian)合并為一(yi)個(ge)文件(jian):cat file1 file2 > file
3.命令參數(shu):
-A, --show-all 等價于(yu) -vET
-b, --number-nonblank 對非空(kong)輸(shu)出行編號(hao)
-e 等價于 -vE
-E, --show-ends 在每行結束處顯示(shi) $
-n, --number 對(dui)輸出(chu)的(de)所有行編號(hao),由1開(kai)始(shi)對(dui)所有輸出(chu)的(de)行數(shu)編號(hao)
-s, --squeeze-blank 有連續(xu)兩行(xing)以上的空(kong)白行(xing),就代換(huan)為一行(xing)的空(kong)白行(xing)
-t 與 -vT 等價
-T, --show-tabs 將跳格字符顯示為 ^I
-u (被忽略)
-v, --show-nonprinting 使用 ^ 和 M- 引用,除(chu)了 LFD 和 TAB 之外
4.使用(yong)實例:
實例一:把 log2012.log 的文件內(nei)容加上行號后輸入(ru) log2013.log 這個文件里
命令:
cat -n log2012.log log2013.log
輸出:
[root@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]#
說明:
實例二:把 log2012.log 和(he) log2013.log 的文(wen)件內容加(jia)上行號(hao)(空白(bai)行不加(jia))之后將內容附(fu)加(jia)到 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]#
實例三:把(ba) log2012.log 的文件內容加上行(xing)號后輸入 log.log 這個文件里
命令:
輸(shu)出:
[root@localhost test]# cat log.log
[root@localhost test]# cat -n log2012.log > log.log
[root@localhost test]# cat -n log.log
1 2012-01
2 2012-02
3
4
5 ======
[root@localhost test]#
實例(li)四:使用(yong)here doc來生成文件
輸出:
[root@localhost test]# cat >log.txt <<EOF
> Hello
> World
> Linux
> PWD=$(pwd)
> EOF
[root@localhost test]# ls -l log.txt
-rw-r--r-- 1 root root 37 10-28 17:07 log.txt
[root@localhost test]# cat log.txt
Hello
World
Linux
PWD=/opt/soft/test
[root@localhost test]#
說明:
注意粗體(ti)部分,here doc可(ke)以進行字符串(chuan)替(ti)換。
備(bei)注:
tac (反向列示)
命令:
tac log.txt
輸出:
[root@localhost test]# tac log.txt
PWD=/opt/soft/test
Linux
World
Hello
說(shuo)明(ming):
tac 是將 cat 反(fan)寫過來(lai),所以他的功能就(jiu)跟 cat 相反(fan), cat 是由(you)第一(yi)行到(dao)最(zui)后(hou)一(yi)行連續顯示(shi)在螢幕上,而 tac 則(ze)是由(you)最(zui)后(hou)一(yi)行到(dao)第一(yi)行反(fan)向(xiang)在螢幕上顯示(shi)出來(lai)!
關注 熵減黑客 ,一起學習成長
