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)
taobaibaitaobaibai
上一篇 2016-08-10 10:27
下一篇 2016-08-10 10:27

相关推荐

  • grep正则表达式

    grep:这是一个搜索命令,搜文本并且将文本行显示出来 (1)grep -i 表示搜索的时候忽略大小写 (2)grep –colour 表示搜索关键字带颜色 例如:grep –colour ‘root’ /etc/passwd 显示为在etc的passwd里的 root 选项有颜色 *为了方便我们可以定义个别名,让他搜索的时候默认显示为带颜色 alias…

    Linux干货 2017-07-29
  • 优云老王(三)谈埋点:人生处处有埋点

    说到埋点,的确是用户最感兴趣的话题之一,优云Web产品在内测阶段便收到了用户的各种反馈,反复问我无码埋点是怎么做的。在此我专门与大家聊聊埋点以及各种实现方案的利弊。 1、埋点的来历 先说下埋点的来历,其实在互联网没流行起来之前,埋点并不是用来分析用户行为的,技术人员为了解决某些问题,就在代码里面加入了些行为逻辑代码,如果用户使用产品的过程中出了问题,就生成一…

    2016-09-19
  • N25-第6周

    vim:    模式化的编辑器        基本模式:            编辑模式,命令模式,            输入模式       &n…

    Linux干货 2017-02-13
  • 计算机和linux基础

    计算机的组成及其功能 计算机硬件主要由5部分组成:运算器 、控制器、存储器、input、output。 但是这和我们平时见到的计算机不一样?我们平时见到的计算机有CPU,内存,硬盘,显示器,鼠标键盘,显卡、主板等。这些东西也都归在以上五类设备中。下面我们简单介绍一下计算机这五个组成部分。 运算器: 运算器是计算机中做运算的部分,可以执行各种指令,加减乘除,与…

    Linux干货 2016-09-16
  • 大概认识linux

             linux简介 其实对Linux系统不是很了解,都是在百度和谷歌搜索出来,才发现Linux很多版本。各版本各优缺点。首先简单说下发展史,Linux在1991年10月5号(这是第一次正式向外公布的时间)在芬兰诞生,以后借助于Internet网络向全世界各地传播,由计算机爱好者的再次开发新功能和…

    Linux干货 2016-05-29
  • linux程序包管理rpm,yum和编译安装以及冒泡排序练习

    linux程序包管理: API:Application Programming Interface POSIX:Portable OS 程序源代码–> 预处理–> 编译–> 汇编–> 链接 静态编译: 共享编译:.so ABI:Application Binary Interface W…

    Linux干货 2016-08-24