目录

  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. Vue仿抽屉

    创建VUE项目的步骤: npm install vue-cli -g vue init webpack myproject cd myproject npm run dev 组件:它是可扩展的html ...

  2. Django复习之ORM

    QuerySet数据类型:                        1.可切片,可迭代      [obj,....]                    2.惰性查询:            ...

  3. 报表嵌入到.net系统页面

    目录: 1. 问题描述 2. 情形一:报表整体嵌入在.net系统框架中 3. 情形二:报表嵌入.net系统的某个页面中 4.权限控制方式 5.嵌入问题解决 1. 问题描述编辑 报表部署到Web应用服务 ...

  4. Qt之创建并使用静态链接库

    1.创建静态链接库 静态库的工程名字 添加包含的模型 更改一下类的名字 我的静态编译库的工程. 写一个简单的静态哭的代码为后面测试静态库使用 cpp代码: #include "staticb ...

  5. Git使用—第一讲:初识版本控制工具

    几乎所有出色的项目都不是一个人完成的,而是由一个团队共同合作开发完成的,这个时候多人之间的代码同步问题就显得异常重要了,因此版本控制工具也就应运而生了.常见的版本控制工具主要有SVN和Git,接下来要 ...

  6. 1-Android开发验证码(使用第三方Mob,注册)

    http://www.mob.com/ 注册以后 可以直接放在 可以下载到手机里面试一下了 后续就不截图了 补充: 自定义签名

  7. Android6.0权限大全和权限分类

    本文转载至: https://blog.csdn.net/qq_26440221/article/details/53097868 自从出了Android6.0权限管理之后,再也不能像以前那样粘贴复制 ...

  8. proxy config (firefox config)

    sudo apt-get install shadowsocks sudo apt-get install polipo 编辑polipo config: sudo vim /etc/polipo/c ...

  9. 内核里面writel(readl)是如何实现的

    writel和readl,这两个个函数实现在操作系统层,有内存保护的情况下,往一个寄存器或者内存地址写一个数据.先说一下writel:   在arch/alpha/kernel/io.c中有 void ...

  10. Shiro的认证授权

    shiro安全框架入门整理 package com.shiro.test; import org.apache.shiro.SecurityUtils; import org.apache.shiro ...