8 文本查看、处理与统计分析

文本查看

cat

cat [OPTION]... [FILE]...
该命令用于正向查看文本文件,但不可分页,一次性显示完成
  • 参数说明

参数 说明
-E 显示行结束符$
-n 对显示出的每一行进行编号
-A 显示所有控制符
-b 非空行编号
-s 压缩连续的空行成一行
-T 显示tab为^I
  • 示例 image

1.png

tac

tac [OPTION]... [FILE]...
该命令用于将文件已行为单位的反序输出,即第一行最后显示,最后一行先显示,不可分页,一次性显示完成(行内顺序不变)
  • 示例 image

2.png

  • 参考rev

[root@centos7 testdir]# cat f2
abcdef
abcdef
[1..10]
1 2 3 4 5 6 7 8 9 10

[root@centos7 testdir]# rev f2
fedcba
fedcba
]01..1[
01 9 8 7 6 5 4 3 2 1

more

more [OPTIONS...] FILE...
 
 1)-d: 显示翻页及退出提示
 2)分页查看文件,不支持向上翻页,空格想下翻页

less

该命令用于一页一页地查看文件或STDIN输出。less 命令是man命令使用的分页器。
  • 翻屏快捷键

    向后翻一屏  :space
    向前翻一屏  :b
    向后翻半屏  :d
    向前翻半屏  :u
    向后翻一行  :enter
    向前翻一行  :k / y
    退出        :q
    跳转到第?行:#
    回到文件首部:1G
    回到文件尾部:G
  • 查找

    /KEYWORD:向后搜索
    ?KEYWORD:向前搜索
    n       :下一个
    N       :前一个
    退出    :q

head

head [OPTION]... [FILE]...
#-------------------------------------------------------------------
# 1) -c #: 指定获取前#字节
#-------------------------------------------------------------------
[root@centos7 testdir]# head -c 10 passwd root:x:0:0

#-------------------------------------------------------------------
# 2) -n #: 指定获取前#行
#-------------------------------------------------------------------
[root@centos7 testdir]# head -n 2 passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

#-------------------------------------------------------------------
# 2) -#  :指定行数
#-------------------------------------------------------------------
[root@centos7 testdir]# head -2 passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

#-------------------------------------------------------------------
# 3) -q  :结果不打印文件名(默认)
#-------------------------------------------------------------------
[root@centos7 testdir]# head -2 -q passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

#-------------------------------------------------------------------
# 4) -v  :结果打印文件名
#-------------------------------------------------------------------
[root@centos7 testdir]# head -2 -v passwd 
==> passwd <==
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

tail

tail [OPTION]... [FILE]...
#-------------------------------------------------------------------
# 1) -c #: 指定获取前#字节
#-------------------------------------------------------------------
[root@centos7 testdir]# tail -c 14 passwd
god is a girl

#-------------------------------------------------------------------
# 2) -n #: 指定获取前#行
#-------------------------------------------------------------------
[root@centos7 testdir]# tail -n 1 passwd 
god is a girl

#-------------------------------------------------------------------
# 3) -#  :指定行数
#-------------------------------------------------------------------
[root@centos7 testdir]# tail -1 passwd 
god is a girl

#-------------------------------------------------------------------
# 4) -f  : 跟踪显示文件新追加的内容,常用日志监控
#-------------------------------------------------------------------
[root@centos7 testdir]# tail -f passwd
liang:x:1000:1000:liang:/home/liang:/bin/bash
wangcai:x:1001:1001:wangcai,tiantang,110,120:/home/wangcai:/bin/csh
god is a girl

#-------------------------------------------------------------------
# 5) 监控日志信息,并将其放到后台,不影响前台执行命令
#-------------------------------------------------------------------
[root@centos7 testdir]# tail -n 0 -f /var/log/messages &    ### 监控并扔到后台
[2] 4497
[root@centos7 testdir]# Aug  5 10:21:29 centos7 root: this is 4test        ### 日志变化后,立刻显示

[root@centos7 testdir]# jobs                                ### 查看任务???
[1]-  Running                 tail -f /var/log/messages &
[2]+  Running                 tail -n 0 -f /var/log/messages &

[root@centos7 testdir]# fg 1                                ### ???
tail -f /var/log/messages
^C

文本抽取与合并

cut

cut [OPTION]... [FILE]...
#-------------------------------------------------------------------
# 1) -d DELIMITER: 指明分隔符,默认tab
# 2) -f FILEDS:
#       #: 第#个字段
#       #,#[,#]:离散的多个字段,例如1,3,6
#       #-#:连续的多个字段, 例如1-6
#       混合使用:1-3,7
# 3) -c 按字符切割
# 4) --output-delimiter=STRING指定输出分隔符
#-------------------------------------------------------------------
[root@centos7 testdir]# tail -1 passwd 
god is a girl

[root@centos7 testdir]# tail -1 passwd |cut -d" " -f1-3 --output-delimiter=%
god%is%a

[root@centos7 testdir]# tail -1 passwd | cut -c10-13 
girl

paste

paste [OPTION]... [FILE]...
#-------------------------------------------------------------------
# -d 分隔符 :   指定分隔符,默认用TAB
# -s        :   所有行合成一行显示
#-------------------------------------------------------------------
[root@centos7 testdir]# echo "life is " > f3
[root@centos7 testdir]# echo "good" > f4
[root@centos7 testdir]# paste -d"*" f3 f4
life is *good

[root@centos7 testdir]# paste -d"*" -s f3 f4
life is 
good

[root@centos7 testdir]# paste f3 f4
life is 	good
[root@centos7 testdir]# paste -s f3 f4
life is 
good

文本统计与分析

wc

用于统计输入的单词总数、行总数、字节总数和字符总数,可对文件或STDIN中的数据运行。
参数 说明
-l 只计数行数
-w 只计数单词总数
-c 只计数字节总数
-m 只计数字符总数
$ wc story.txt 
 39   237    1901   story.txt
行数 单词数  字符数    文件名

sort

sort [options] file(s)
把整理过的文本显示在STDOUT,不改变原始文件
参数 说明
-r 执行反方向(由上至下)整理
-n 执行按数字大小整理
-f 选项忽略(fold)字符串中的字符大小写
-u 选项(独特,unique)删除输出中的重复行
-t c 选项使用c做为字段界定符
-k X 选项按照使用c字符分隔的X列来整理能够使用多次
#-------------------------------------------------------------------
# 将passwd用:分隔,将第三列按照数字进行排序
#-------------------------------------------------------------------
[root@centos7 testdir]# cat passwd | sort -n -t: -k3
root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

image3.png

uniq

uniq [OPTION]... [FILE]...
uniq命令:从输入中删除重复的前后相接的行,常和sort 命令一起配合使用:sort  userlist.txt  |  uniq -c
参数 说明
-c 显示每行重复出现的次数;
-d 仅显示重复过的行;
-u 仅显示不曾重复的行;连续且完全相同方为重复
[root@centos7 testdir]# cat /etc/init.d/functions |tr -cs "[:alpha:]" '\n'|sort|uniq -c|sort -nr
     67 pid      55 if
     54 file     51 echo

[root@centos7 testdir]# df|tr -s " " |sort -nr -t" " -k5|cut -d" " -f5
100%
71%
4%
1%
1%
Use%
0%

[root@centos7 testdir]# cat passwd |sort -nr -t: -k3|head -1|cut -d: -f1,3,7
nfsnobody:/sbin/nologin

diff & patch

diff用于比较两个文件的差异,patch则可以用于通过差异和一个文件恢复另一个文件。
这两个命令与git的命令很类似,可用于版本管理。
[root@centos7 testdir]# echo "god is a girl." > f1
[root@centos7 testdir]# echo "got is A garl." > f2
[root@centos7 testdir]# diff f1 f2 |tee diff        ### 比较f1与f2的差异1c1
< god is a girl.
---
> got is A garl.

[root@centos7 testdir]# diff -u f1 f2 > diff        ### 比较f1,f2,并将差异导出为patch文件
[root@centos7 testdir]# cat diff                    
--- f1	2016-08-08 16:22:35.575006634 +0800         ### -代表f1
+++ f2	2016-08-08 16:22:52.175007341 +0800         ### +代表f2
@@ -1 +1 @@                                         ### 第一行有不同
-god is a girl.                                     ### f1的第一行内容
+got is A garl.                                     ### f2的第一行内容
[root@centos7 testdir]# rm -f f2                    ### 删除f2
[root@centos7 testdir]# cp f1 f1.bak                ### 将f1备份
[root@centos7 testdir]# patch -b f1 diff            ### 通过f1、diff恢复f2,恢复后名为f1,覆盖原f1,故要备份
bash: patch: command not found...                   ### CentOS7可以man出patch命令,但不可执行,CentOS6可以
[root@localhost ~]# patch -b f1 diff                ### CentOS6执行patch恢复的结果
patching file f1
[root@localhost ~]# cat f1
got is A garl.

原创文章,作者:taobaibai,如若转载,请注明出处:https://www.178linux.com/31592

赞 (0)
taobaibaitaobaibai
上一篇 2016-08-10 10:27
下一篇 2016-08-10 10:27

相关推荐

  • 网络21期第十周博客作业

    1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情) Centos6启动流程:       POST加电自检 —> BOOT Sequence —> 加载内核启动系统    &n…

    Linux干货 2016-09-15
  • 简明 Vim 练级攻略

    vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的。下面的文章翻译自《Learn Vim Progressively》,我觉得这是给新手最好的VIM的升级教程了,没有列举所有的命令,只是列举了那些最有用的命令。非常不错。 ——————————正文开始—————————— …

    Linux干货 2015-04-04
  • 查找find命令简介

       之前都是写一些关于课后练习题的博客,既练习了课后习题也把博客写了,舒服!不过这周学的知识需要练习的不多,更多的是需要了解和记忆,所以给大家介绍一个自我觉得挺强大的一个命令,那就是查找find命令。大家都知道计算机的数据茫茫多,要想找到自己想要的级必须加点限制性的条件来缩小一下范围,或者准确的告诉计算机你想要的是什么。那么怎么来实现呢…

    Linux干货 2017-08-12
  • Find命令的使用

    Find命令的基本用法 在Linux系统中find命令是一个强大文件查找类工具,它支持根据文件名,权限,文件大小,访问以及修改时间查找,使用方法如下: find [option] [查找起始路径] [查找条件] [处理动作] 起始路径:指定查找文件或目录的路径,默认为当前目录 查找条件:可根据文件名,权限,文件大小以及从属关系查找文件 处理动作:动查找到的文…

    Linux干货 2017-08-28
  • 初窥门径之脚本书写

    一、几个脚本的编写: 1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。 #!/bin/bash echo "HOSTNAME: $HOSTNAME" echo "IPV4 A…

    Linux干货 2016-08-15
  • 用户,组和权限管理 基础命令整理

    安全3A任何一个安全策略都应该有Authentication(认证)、Authentication(授权)、Accouting|Audition(审计)这三个措施。 用户(User)Linux用户是安全3A中的Authentication(认证)措施。用户在登录时,系统会检查用户输入的用户名和密码。如果用户输入的用户名和密码匹配,会根据用户信息给用户分配一个…

    Linux干货 2017-03-16