shell脚本--显示文本内容

shell脚本显示文本内容及相关的常用命令有cat、more、less、head、tail、nl

 

首先是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命令

 

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命令加一个数字表示一次显示几行。

 

 less命令:与more命令很相似,但是用法比较单调,一次性显示一屏,然后使用和more一样的快捷键:

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

 

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

  

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

  

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

  

 

转载于:https://www.cnblogs.com/-beyond/p/8242905.html

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值