开发者学堂课程【Linux企业运维实战 - 入门及常用命令:Linux面试必考-高级文本处理工具进阶】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/550/detail/7616
Linux面试必考-高级文本处理工具进阶
内容简介
一、paste
Paste不是从一个文件中取多个列,而是合并两个文件。
paste合并两个文件同行号的列到一行
paste [OPTION]...[FILE]...
-d分隔符:指定分隔符,默认用TAB
-s∶所有行合成一行显示
paste f1 f2
paste -s f1 f2
横向合并:
[r
oot@centos6 ~j#cat f
1
1
2
3
4
5
6
7
8
9
10
[root@centos6~]#cat f2
10
11
12
13
14
15
16
17
18
19
20
[root@centos6~]#paste f1 f2
1 10
2 11
3 12
4 13
5 14
6 15
7 16
8 17
9 18
10 19
第一个文件的第一行和第二个文件的第一行横向合成了一行。
纵向合并:
[root@centos6~]#cat !
* c
at f1 f2
1
2
3
4
5
6
7
8
9
10
10
11
12
13
14
15
16
17
18
19
20
Paste-s的合并方式:
[root@centos6 ~]#paste
-s
f1
f2
1 2 3 4 5 6 7 8 9 10
10 11 12 13 14 15 16 17 18 19
Paste -s的合并方式:
[root@centos6 ~]#paste -d
”
:
”
f1 f2
1:10
2:11
3:12
4:13
5:14
6:15
7:16
8:17
9:18
10:19
:20
二、分析文本的工具
1.文本数据统计: wc(word count)
[root@centos6 ~]#wc f1
10(文件的行数) 10(文件有几个单词,只要不用空格隔开都默认算一个单词)
21(文件有几个字节) f1
WC并不是常用在文本场景,常用的场景是和管道配合起来用,因为WC是支持标准输入的。
如果想统计当前目录下的文件个数:
[root@centos6 ~]#ls
Anaconda-ks.cfg Documents f1 install.log Music Public Videos
Desktop Downloads f2 install.log.syslog Picture Templates
[root@centos6-]#
ls | wc
13 13 120(字节)
WC命令有自己的程件选项,比方说可以统计有多少行,行用的是-l,统计指定文件的行数:wc -1 /etc/passwd,正常情况下,一行代表了一个账号。
统计有多少个单词:wc -w/etc/passwd
。
统计字节数:wc -c/etc/passwd
统计字符数:wc -m /etc/passwd
显示最长的行:wc - L/etc/passwd
统计账号有多少用户在登录:who |wc -1
统计日志:cat /var/log/httpd/access_log|wc -1
统计网站访问量的一个指标:pv page view
总结:计数单词总数、行总数、字节总数和字符总数
可以对文件或 STDIN 中的数据运行
wc story.txt
39 237 1901 story.txt
行数 字数 字节
数常用选项
-l只计数行数
-w
只计数单词总数
-c
只计数字节总数
-m
只计数字符总数
-L
显示文件中最长行的长度
2.文本排序: sort
把整理过的文本显示在 STDOUT,不改变原始文件
Sort
[
options] file(s)
指定对第几列排序:sort -t:-k/etc/passwd
(指定对第一列排序)
如果是对数字排序不是按照数字大小牌组,还是按照字符进行排序。如果想按照数字大小进行排序则输入:sort -t:-k3 -n /etc/passwd
反向排序:sort -t:-k3 -nr /etc/passwd
查找分区利用率最大的值:
[root@centos6 ~]#df | tr -s
‘
‘
%|cut -d% -f5
Use
10
1
4
1
100
[root@centos6 ~]#df |tr -s
‘
‘
%|cut -d% -f5 |sort -nr|head -n1
[root@centos6~]#netstat-nt
Active Internet connections (w/o servers)
Proto Recv-o send-o Local Address Foreign Address State
Tcp 0 96 192.68.30.128: 22 192.168.30.1:53835 ESTALISHED
PID程序编号
排序:cut -d
“
“
-f1/var/log/httped/access_log
显示出来以后,默认按照访问的顺序排列。
要把相同的放到一起,所以进行排序:cut -d “ “-f1 /var/log/httpd/access_log |sort,整理过之后,同一个IP就放在一起。
◆常用选项
➢-r 执行反方向(由上至下)整理
➢-R随机排序
➢-n 执行按数字大小整理
➢-f选项忽略( fold )字符串中的字符大小写
➢-u 选项(独特, unique )删除输出中的重复行
➢-t c选项使用c做为字段界定符
➢-k x选项按照使用c字符分隔的x列来整理能够使用多次
倒序排序:seq 1 20 |sort -nr
20
19
18
17
16
15
随机排序:seq 1 20 |sort -R(可以利用这个进行抽奖)
34
6
15
20
取用户名和ID,并且对ID进行倒序排序
Cut -d:-f1,3/etc/passwd|sort -t :-k2 -nr
3.uniq命令
◆uniq命令:从输入中删除前后相接的重复的行
◆uniq [OPTION]... [FILE]...
-C:显示每行重复出现的次数
-d:仅显示重复过的行
-U:仅显示不曾重复的行
注:连续且完全相同方为重复
常和 sort 命令一起配合使用 :
sort userlist.txt| uniq -C
假如说生成一个文件:
[root@centos6 ~]#cat f1
aa
aa
bb
aa
Ccc
Ccc
Ddd
[root@centos ~]#uniq f1
aa
cc
aa
Ccc
ddd
ccc
ddd
此时 uniq 的作用就是把相邻的重复行合并为一行
想知道是几个行合并成一个行的操作方法:
[root@centos ~]#uniq -c f1
2 aa
1 bb
1 aa
1 ccc
1 ddd
2 ccc
1 ddd
如果想要统计日志中有多少IP分别访问过我,而且统计出来每个IP访问了多少次。
首先取出IP:[root@centos ~]#cat /var/log/httpd/access-log |cut -d” “-f1
排序:[root@centos ~]#cat /var/log/httpd/access-log |cut -d” “-f1|sort
统计相邻的行重复了多少次:[root@centos ~]#cat /var/log/httpd/access-log |cut -d” “-f1|sort|uniq -c
取次数出现最多的前十个:[root@centos ~]#cat /var/log/httpd/access-log |cut -d” “-f1|sort|uniq -c|sort -nr|head -n10
Last命令也是一个显示日志的命令,reboot表示重启的信息
作业:统计一下远程的主机登录次数最多的前三个IP地址
4.比较文件:diff和patch
◆比较两个文件之间的区别
diff foo.conf foo2.conf
5c5
< use_ _widgets = no
---
>use_ widgets = yes
➢注明第5行有区别(改变)
如果想要比较两个文件是否一样,不能仅仅比较文件的大小,还要关注文件的内容,
[root@centos6 ~] #
[root@centos6 ~] #
[root
@centos6 ~
]
#
[root@
centos6 ~
]
#11 /data/
f1
lost+ found/
[root@
centos6 ~]#cp /data/f1 /data/f2
[root@centos6 ~] #nano /data/f2
-r--rw-rwx. 1 wang g1
2
Apr
4 17:50 /data/f1
一r--r--r-x. 1 root
r
oot 2
Apr
4 17:50 /data/f2
[root@centos6 ~ ]#diff *
diff /data/f1 /data/f2
1c1
---
>a
[root@centos 6 ~]#cp/etc/fstab
[root@centos6 ~]nano/etc/fstab
[root@centos6 ~]diff /etc/fstab fstab
4c4
< # Created by anaconda on Tue Mar 27 16:59:39 2018
---
> # eated by anaconda on Tue Mar 27 16:59:39 2018
9c9
< UUID=9c3da07 a- ac88 -449b- a3be-6f1d638ca49c /
ext4
defaults
11
---
>
UaaaID=9c 3da07 a- ac88 -449b-a3be-6f1d638ca49c /
ext4
defaults
11
Diff-u的使用:
[root@centos6 ~] #diff -u /etc/fstab
fstab
---
/etc/fstab
2018-03-27 16:59: 39.943999088 +0800
+++ fstab
2018-04-04 17 :51:27.907844698 +0800
aa -1,12 +1,12 qa
Diff -u fstab fstab2 > diff.log的好处就是,当以文件丢失时,可以利用这个方法找回这个文件。但是找回来的文件并不是旧文件的名字。
复制对文件改变patch
◆diff
命令的输出被保存在一种叫做“补丁”的文件中
➢使用
选项来输出“统一 -的 ( unified )”diff格式文件,最适用于补J文件
◆patch复制在其它文件中进行的改变 (要谨慎使用)
➢适用-b 选项来自动备份改变了的文件
$ diff -u foo.conf foo2.conf > foo. patch
$ patch b foo.conf foo.patch
[root@centos6 ~]#a+b^g
[root@centos6 ~]#rm -f fstab
[ r oot@centos6 ~ ] #patch -b
fstab2 diff. log
Patching
file fstab2
Reversed (or previously applied) patch detected!
Assume -R? [n] y
[root@centos6 ~]#11 fstab2
-rw-r--r--. 1 root root 900 Apr 4 17 :55 fstab2
[root@centos6 ~]#11 fstab2
-rw-r--r--. 1 root root 900 Apr 4 17 :55 fstab2
Downloads f2 fstab2.orig install.log.syslog Picture Templates
Desktop Documents f1 fastab2 install/log Music Public Videos
三、练习
◆找出 ifconfig “网卡名” 命令结果中本机的IPv4地址
◆查出分区空间使用率的最大百分比值
◆查出用户 UID 最大值的用户名、UID及shell类型
◆查出 /tmp 的权限,以数字方式显示;
◆统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序(netstat -nt)
四、Linux文本处理三剑客
◆grep : 文本过滤(模式: pattern)工具
grep, egrep, fgrep (不支持正则表达式搜索)
◆sed : stream editor ,文本编辑工具
awk : Linux上的实现gawk ,文本报告生成器