shell腳本--顯示文本內容


目錄

  1、cat

  2、more

  3、less

  4、head & tail

  5、nl

  6、tee

 

 

 

 

 

 

 

 

  shell腳本顯示文本內容及相關的常用命令有cat、more、less、head、tail、nl

 

1、首先是cat

  cat最常用的就是一次性顯示文件的所有內容,如果一個文件的內容很多的話,那么就不是很方便了,所以一般用於查看內容比較少的文本文件;

  cat另外一個很有用的方法就是可以原樣輸出想要保留特定格式的內容。

[root@localhost ~]# cat <<A
> this is test
>     hello world
>           hello Linux  PHP   MySQL  Apache Nginx
> A
this is test
    hello world
          hello Linux  PHP   MySQL  Apache Nginx
[root@localhost ~]#

  其中<<后面隨意跟一個字母,然后在結束那一行的首字母以這個字母結束,即可原樣輸出內容。

因為cat是一次性的顯示所有內容不方便,所以出現了more命令

 

2、more命令

  more命令,它可以指定顯示多少行(不指定時,默認顯示一屏),注意只能

  • 按Space鍵或者按 f 鍵:顯示文本的下一屏內容。
  • 按Enier鍵:只顯示文本的下一行內容。
  • 按b鍵:顯示上一屏內容。
  • 按q鍵:退出顯示內容操作
[root@localhost ~]# more -5 a.txt
this is 1
this is 2
this is 3
this is 4
this is 5
--More--(2%)

  more命令加一個數字表示一次顯示幾行。

 

 3、less命令

  less與more命令很相似,但是用法比較單調,一次性顯示一屏,然后使用和more一樣的快捷鍵:

  • 按Space鍵或者按 f 鍵:顯示文本的下一屏內容。
  • 按Enier鍵:只顯示文本的下一行內容。
  • 按b鍵:顯示上一屏內容。
  • 按q鍵:退出顯示內容操作

 

4、head命令和tail命令

  顧名思義,head顯示文件的前一部分內容,tail顯示文件末尾部分的內容。在不指定顯示的行數時,都默認為10行

[root@localhost ~]# head a.txt
this is 1
this is 2
this is 3
this is 4
this is 5
this is 6
this is 7
this is 8
this is 9
this is 10
[root@localhost ~]# head -5 a.txt
this is 1
this is 2
this is 3
this is 4
this is 5
[root@localhost ~]#

  

5、nl命令

  顯示文件的內容,並且每一行的行首會顯示行號。

[root@localhost ~]# nl a.txt
     1  this is 1
     2  this is 2
     3  this is 3
     4  this is 4
[root@localhost ~]# head -3 a.txt
this is 1
this is 2
this is 3
[root@localhost ~]# head -3 a.txt | nl
     1  this is 1
     2  this is 2
     3  this is 3
[root@localhost ~]#

  

6、tee命令

  tee命令在單獨使用的時候,可以將鍵盤輸入的內容重定向(存入)后面的文件中,注意是以覆蓋重定向(是>而不是>>),以ctrl+c結束輸入。

[root@localhost ~]# cat a.txt
[root@localhost ~]# tee a.txt
hello world
hello world
This is shell script
This is shell script
^C
[root@localhost ~]# cat a.txt
hello world
This is shell script
[root@localhost ~]# tee a.txt
will clear the content
will clear the content
^C
[root@localhost ~]# cat a.txt
will clear the content
[root@localhost ~]#

  於是乎,tee可以與其他命令(如上面的各個命令)一起配合使用,在顯示前一條命令的結果的同時,將內容保存一份(即將內容重定向到文件中),達到既顯示內容,又保存內容的目的。

[root@localhost ~]# head -5 a.txt | tee b.txt
this is 1
this is 2
this is 3
this is 4
this is 5
[root@localhost ~]# cat b.txt
this is 1
this is 2
this is 3
this is 4
this is 5
[root@localhost ~]# tail -5 a.txt | tee c.txt | nl
     1  this is 197
     2  this is 198
     3  this is 199
     4  this is 200

[root@localhost ~]# cat c.txt
this is 197
this is 198
this is 199
this is 200

[root@localhost ~]#

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM