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

相关推荐

  • Python语法基础之if while for常见的简单算法

    格式要符合语法要求

    2017-09-14
  • Linux的哲学思想及Linux发行版

    讲述一下几个问题: 第一,Linux的哲学思想;第二,Linux发行版的基础目录名称,命名法则和功用规定;第三,Linux不同发行版之间的联系和区别。 Linux的哲学思想: 1、一切皆文件。是Unix/Linux的基本哲学之一,不仅普通的文件,目录,字符设备,块设备,套接字等在Unix/Linux中都是以文件被对待。 2、小型,单一用途的程序。程序和可执行…

    Linux干货 2016-09-17
  • N25-第一周 总结

    linux bassic The first week of blogging 概要 计算机与操作系统、linux发行版及他们之间联系与区别、Linux的哲学思想、linux系统上命令使用格式及基础命令介绍、linux命令帮助说明、FHS 一、计算机与操作系统 什么是计算机? 电子计算机(computer),亦称电脑,是一种利用电子学原理,根据一系列指令对数…

    Linux干货 2016-12-04
  • 学习宣言

    新的一天开始了, 从今天起,正式开始Linux的系统学习, 对于基础薄弱的我来说,是一个新的挑战,而我接受这个挑战。 在今后的日子里,一定会拼搏奋进,更上一层楼。 积土而为山,积水而为海, 定会一天比一天强,努力吧。

    Linux干货 2016-10-24
  • 解决Redis 延迟故障

    前一段时间redis客户端在使用php connect 连接redis 的经常报一个redis server went away 等信息。 首先想到的想到的是reids超时设置的问题,timeout、tcp-keepalive、以及php的default_socket_timeout时间  127.0.0.1:6381> CONFI…

    Linux干货 2016-02-14
  • 马哥linux0803作业内容

    1. 创建sysadmins组 将用户user1,user2,user3加入sysadmins组中 将user3设置为sysadmins的管理员 用user3登录,将user2从组中移除 设置sysadmins的密码centos 设置user1 在创建新文件时,文件的所属组为sysadmins 删除user1…3 删除sysadmins 2、三种权限rwx对…

    Linux干货 2016-08-04