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

相关推荐

  • PXE自动化安装系统服务

    PXE PXE(preboot execute environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统,在启动过程中,终端要求服务器分配IP地址,再用TFTP(trivial file transfer protocol)或M…

    Linux干货 2016-11-07
  • linux系统自动化安装和selinux

    系统自动化安装: Anaconda 安装系统分成三个阶段:  安装前配置阶段安装过程使用的语言键盘类型安装目标存储设备Basic Storage :本地磁盘特殊设备:iSCSI设定主机名配置网络接口时区管理员密码x设定分区方式及MBR 的安装位置创建一个普通用户选定要安装的程序包 创建引导光盘:#cp /media/cdrom/isoli…

    Linux干货 2017-04-06
  • linux中find命令的那些事

         概述:在介绍find这个强大的文件查找命令前,我们先介绍两个较简单的文件查找命令whereis和locate命令,通常我们都是先使用whereis或者是locate检查,如果真的找不到了,才用find来查找,因为whereis和locate是利用数据库来查找数据的,速度相当快,节约时间,缺点就是有可…

    Linux干货 2016-08-18
  • N28-第四周博客作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。

    2、编辑/etc/group文件,添加组hadoop。

    3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/hadoop。

    4、复制/etc/skel目录为/home/hadoop,要求修改hadoop目录的属组和其它用户没有任何访问权限。

    5、修改/home/hadoop目录及其内部所有文件的属主为hadoop,属组为hadoop。

    6、显示/proc/meminfo文件中以大写或小写S开头的行;用两种方式;

    7、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;

    8、显示/etc/passwd文件中其默认shell为/bin/bash的用户;

    9、找出/etc/passwd文件中的一位数或两位数;

    10、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;

    11、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;

    12、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;

    13、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;

    Linux干货 2017-12-26
  • Bash学习基础知识一“命令”

    Bash 学习基础知识 目录 一、Shell是个啥? 二、BASH的命令     2.1 命令的语法格式     2.2 什么是命令     2.4 命令的类型     …

    Linux干货 2015-04-03
  • Ansible

    Ansible简介 ansible是一种基于python语言开发的轻量级自动化运维工具,它可以自动化批量完成主机服务配置管理,软件部署,执行特定命令等工作 ansible的核心组件有ansible core(核心代码),host inventory(要管理的主机),core modules(核心模块),custom modules(用户可以自定义模块),pl…

    Linux干货 2016-11-14