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 ~]#

  

 

posted @ 2018-01-09 00:19  寻觅beyond  阅读(8400)  评论(2编辑  收藏  举报
返回顶部