目录

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

  

shell脚本--显示文本内容的更多相关文章

  1. iOS使用shell脚本注入混淆内容

    背景 公司需要做一系列的壳版本,壳版本如果内容雷同提交到App Store会有被拒绝的风险,其中有一种解决方案是在壳版本中注入混淆的代码,防止被苹果检测到内容太过雷同而导致审核被拒绝,本文是针对这个场 ...

  2. shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中

    shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中 利用shell脚本将文本数据导入到mysql中 需求1:处理文本中的数据,将文本中的数据插入到mys ...

  3. Shell脚本学习 - 基本内容以及数据格式

    为了捞取日志,自己用python写了一些东西,大致套路就是读取写入文件的操作,放到linux上跑.实际使用时发现要操作的文件有时比较大,直接打开文件找需要的东西可能会有一些效率问题.所以学习一下she ...

  4. shell脚本显示字体颜色

    shell脚本中echo显示内容带颜色显示,echo显示带颜色,需要使用参数-e 格式如下: echo -e "\033[字背景颜色:文字颜色m字符串\033[0m" 例如: ec ...

  5. 如何把rc.local里执行的shell脚本的日志内容放到其他位置

    rc.local的日志内容默认是/var/log/boot.log /etc/rc.d/rc.local文件的文件头是#!/bin/sh ,我们把这修改成#!/bin/sh -x,这样系统启动后就会把 ...

  6. Shell脚本——显示系统上的登录用户数

    写一个脚本showlogged.sh,其用法格式为: showlogged.sh -v -c -h|--help 其中,-h选项只能单独使用,用于显示帮助信息:-c选项时,显示当前系统上登录的所有用户 ...

  7. linux(centos8):用cut显示文本内容的指定列

    一,cut命令的用途 从一个文本文件或者文本流中提取文本列 分别用: 字节.字符.字段 作为单位进行提取 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.co ...

  8. shell脚本修改文本中匹配行之前的行的方法

    原创文件,欢迎阅读,禁止转载. 例子中是把 finish 前一行的 "yes" 改成 "YES"有一个方法就是利用sed+awk一起来完成. zjk@zjk:~ ...

  9. shell 脚本中将输出内容赋值给一个变量时的换行问题

    for ((i=0; i<4; i++) do ....... done 如果使用 echo $a 输出变量,则变量中的 换行都会被忽略掉,所有内容输出到一行 而使用 echo "$a ...

随机推荐

  1. Eclipse字体最好

    改变字体 windows->Preferences->General->Appearance->Colors and Fonts 右边选择Java ->Java Edit ...

  2. Memcached服务加固方案

    配置访问控制.建议用户不要将服务发布到互联网上而被黑客利用,可以通过ECS安全组规则或IPtables配置访问控制规则.例如,在Linux环境中运行命令,在IPtables中添加此规则只允许192.1 ...

  3. Color the ball HDU - 1556 (非线段树做法)

    题意:在1到n的气球中,在不同的区域中涂颜色,问每个气球涂几次. #include<cstdio>int num[100010];int main(){ int n, x, y;; whi ...

  4. 转载 Spring详细教程

    SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于Spring ...

  5. 转载 js函数声明和函数表达式

    在js中函数有两种表达方式.1 函数声明 2 函数表达式 函数声明 function sayname(){ alert("li lei"); } 函数表达式 var sayname ...

  6. gas问题out of gas的解决

    1.昨天遇见了还是以前遇见的问题,就是发现有些函数就是不能用web3调用,然后怎么弄都写不到数组上,但是今天终于将它解决了web3的学习:https://github.com/ethereum/wik ...

  7. mysql order by 排序的问题

    参考博客http://blog.csdn.net/hollboy/article/details/13296601 mysql order by 的排序在今天时候遇到了问题 情景是:将排序的字段设置成 ...

  8. Luogu P4779 【模板】单源最短路径(标准版)(Dijkstra+堆优化模板)

    qwq dij其实和prim挺像的,prim是找权值最小点,dij是找边, 用一个优先队列就可以在加入边的时候直接排序,避免了每次遍历更新min priority_queue <pair< ...

  9. Subversion 1.8.9 ( SVN Client ) 安装最新版本的svn客户端

    For CentOS7 Users: [WandiscoSVN] name=Wandisco SVN Repo baseurl=http://opensource.wandisco.com/cento ...

  10. Android之TCP服务器编程

    推荐一个学java或C++的网站http://www.weixueyuan.net/,本来想自己学了总结出来再写博客,现在没时间,打字太慢!!!!,又想让这好东西让许多人知道. 关于网络通信:每一台电 ...