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,如若转载,请注明出处:http://www.178linux.com/31592

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

相关推荐

  • 马哥教育网络班22期-第6周博客作业

    1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; [root@centos6 ~]# cp /etc/rc.d/rc.sysinit /tmp/ [root@centos6 ~]# sed -…

    Linux干货 2016-12-05
  • sed工具 介绍

    sed工具 地址定界 不给地址:对全文进行处理 单地址: #:指定行 /pattern/:被此处模式所能够匹配到的每一行 地址范围: #,# #,+# /pat1/,/pat2/ #,/pat1/ ~:步进 1~2 奇树行 2~2 偶数行 cat -n passwd >passwd2 新建文件 sep -n ‘/^h//^s/’…

    Linux干货 2017-05-31
  • 说说web和http以及lamp/lnmp

    <span style="font-size: 24px;font-family: 宋体,SimSun">**说说web和http以及lamp/lnmp**</span> 说说web和http以及lamp/lnmp 在这篇文章当中你将看到如下内容: web与http的简介,网页的分类 度量网站流量的几个术语——知道…

    2016-05-28
  • 磁盘管理

    磁盘管理 本文将按顺序以实例演示磁盘管理的所有操作,让我们开始吧! 一,磁盘的添加 ① 先来查看linux系统总共有几个磁盘,由图可知是两个,分别是sda,sdb。我们再加一个,按照磁盘命名顺序,应是sdc,他们都在/dev目录下。 补充: 1,磁盘命名规则: 不同磁盘,按照a-z依次标识,如sda,sdb,sdc 同一磁盘的不同分区,按照1,2,&#823…

    2017-08-19
  • 硬盘分区MBR和GPT选哪个好?有什么区别?

    当前主流的硬盘分区方式有两种:MBR和GPT。 一、MBR与GPT简介与结构 什么是MBR?         MBR,全称为Master Boot Record,即硬盘的主引导记录。是对IBM兼容机的硬盘或者可移动磁盘分区时,在驱动器最前端的一段引导扇区。 MBR的组成部分       &…

    Linux干货 2016-08-29
  • ​Linux基础知识之screen命令详解

    该博文以CentOS6.8_x86_64系统为基础,Xshell5远程登录CentOS6.8系统,以root身份登录系统,以Xshell5发起screen帮助,用系统命令行界面去连接。 screen命令:         打开新的screen: screen -S [SESSION]   (打开一个名叫hel…

    Linux干货 2016-07-29