cat命令
cat命令的用途是連接文件或標準輸入並打印。這個命令常用來顯示文件內容,或者將幾個文件連接起來顯示,或者從標準輸入讀取內容並顯示,它常與重定向符號配合使用。
1.命令格式
cat [選項] [文件]…
2.命令功能
cat主要有三大功能:
- 一次顯示整個文件:
cat filename - 從鍵盤創建一個文件:
cat > filename只能創建新文件,不能編輯已有文件. - 將幾個文件合併爲一個文件:
cat file1 file2 > file
命令參數:-
-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.使用實例
實例一
把 mylog1.log 的文件內容加上行號後輸入 mylog2.log 這個文件裏。
命令:
cat -n mylog1.log mylog2.log
輸出:
[yiibai@localhost test]$ cat mylog1.log
this is line 1
this is line 2
[yiibai@localhost test]$ cat mylog2.log
log2 this is line 1
log2 this is line 2
log2 this is line 3
[yiibai@localhost test]$ cat -n mylog1.log mylog2.log
1 this is line 1
2 this is line 2
3 log2 this is line 1
4 log2 this is line 2
5 log2 this is line 3
[yiibai@localhost test]$
實例二
把 mylog1.log 和 mylog2.log 的文件內容加上行號(空白行不加)之後將內容附加到 log.log 裏。
命令:
cat -b mylog1.log mylog2.log log.log
輸出:
yiibai@localhost test]$ cat -b mylog1.log mylog2.log >> log.log
[yiibai@localhost test]$ cat log.log
1 this is line 1
2 this is line 2
3 log2 this is line 1
4 log2 this is line 2
5 log2 this is line 3
[yiibai@localhost test]$
實例三
使用here doc來生成文件
[yiibai@localhost test]$ cat >log.txt <<EOF
> Hello
> World
> Linux command
> PWD=$(pwd)
> EOF
[yiibai@localhost test]$ ls -l log.txt
-rw-rw-r--. 1 yiibai yiibai 49 Feb 13 03:20 log.txt
[yiibai@localhost test]$ cat log.txt
Hello
World
Linux command
PWD=/home/yiibai/test
[yiibai@localhost test]$