文本处理工具介绍

这篇文章主要介绍一些文本处理工具,包括less、cat、head、tail、cut
基础不牢,地动山摇

cat命令

作用:显示文本内容
选项:

-A:显示所有特殊字符,包括空格、windows中的^M
-E:显示行结束符$
-n: 对显示出的每一行进行编号
-b:非空行编号
-s:压缩连续的空行成一行
  • -A
在windows下新建一个文件,编辑内容如下:
aa
bb
cc
使用rz命令上次到linux
[root@centos7 data ]#cat -A test.txt 
aa^M$                                    ##显示^M的特殊字符,这表示windows中的回车;$表示行结束符,等同于-E.
bb^M$
cc[root@centos7 data ]#
扩展:在windows中的换行符由回车和换行即\r\n表示,\r表示回车,\n表示换行,而在linux中的换行符仅用\n表示,于是多出来的\r被解释成了^M,在linux 编辑文件时发现^M,可以确定在windows中编辑过,特别是在允许脚本的时候,方便排错
  • cat编辑文件
[root@centos7 data ]#cat  f1
a
b
c                             ##   Ctrl+d结束并退出
[root@centos7 data ]#cat f1
a
b
c

[root@centos7 data ]#cat > f2 <<EOF
> XIN
> YUANHONLI
> HAH
> EOF                                  ## 末尾和上面的EOF要对应,也可以使用其他的单词表示
[root@centos7 data ]#cat f2
XIN
YUANHONLI
HAH

tac

即,cat命令反过来,
作用:垂直方向,倒过来显示

cc[root@centos7 data ]#cat test
aa 
bb
cc 
dd 
[root@centos7 data ]#tac test
dd 
cc 
bb
aa 

rev

水平方向,倒过来显示

[root@centos7 data ]#cat test 
aabbcc
[root@centos7 data ]#rev test
ccbbaa

head

作用:显示文本前#行内容
语法:head [OPTION]... [FILE]...
选项:
-c #:指定获取前#字节
-n #: 指定获取前#行
-# :指定行数

[root@centos7 data ]#head -c 3 test
aab[root@centos7 data ]#

[root@centos7 data ]#head -n 3 /etc/fstab 

#
# /etc/fstab

使用head -c取随机数(要求:大小写字母、数字,10位长度)
[root@centos7 data ]#cat /dev/urandom          ## urandom是一个设备,存储随机数
[root@centos7 data ]#tr -dc "[[:alnum:]]" < /dev/urandom |head -c 10    ##使用tr删除除字母、数字的所有字符,然后head取
si8eE8JYSI[root@centos7 data ]#

tail

作用:显示文本后#行内容
语法:tail [OPTION]... [FILE]...
选项:
-c #:指定获取后#字节
-n #:指定获取后#行
-# : 指定行数
-f :跟踪文件描述符,常用于日志监控
-F :跟踪文件名
tailf类似tail -f

-f 和 -F的区别:-F是跟踪文件名,一旦文件被删除,则停止跟踪。而-f即使文件被删除,也还跟踪。

[root@centos7 data ]#cat test 
aabbcc
[root@centos7 data ]#tail -c 2 test
c                                        ## 默认会将末尾的换行符也当成一个字节

[root@centos7 data ]#cat f1
a
b
c

[root@centos7 data ]#tail -c 1 f1

[root@centos7 data ]#tail -c 2 f1
c                                          ## 同上,末尾的换行符也是一个字节     
[root@centos7 data ]#tail -c 3 f1

c

cut

作用:根据分隔符,取特定的列
cut [OPTION]... [FILE]...
选项:
-d delimiter:指定分隔符,默认是tab
-f fileds:
#:第#列
#,#[,#]:不连续的多个列,例如1,3,6
#-#:连续的多列,例如1-6
混合使用:1-3,7
-c:按字符数取列
--output-delimiter=string:指定输出的分隔符,即自定义分隔符

在/etc/passwd文件中取出所有用户的UID?
[root@centos7 data ]#cut -d: -f3 /etc/passwd
0
1
2
3
4
5
6
7
8
11
12

取出磁盘利用率
[root@centos7 data ]#df |tr -s ' ' |cut -d " " -f5|cut -d% -f1      ##因为空格不只一个,所以使用tr -s压缩为一个空格,然后再取
Use
7
0
0
2
0
1
16
1
100
或者直接使用tr压缩时替换分隔符为%,直接取
[root@centos7 data ]#df |tr -s ' ' % |cut -d% -f5
Use
7
0
0
2
0
1
16
1
100

自定义分隔符
[root@centos7 data ]#cut -d: -f1,3 --output-delimiter=+  /etc/passwd
root+0
bin+1
daemon+2
adm+3

[root@centos7 data ]#cut -d: -f1,3 --output-delimiter===  /etc/passwd
root==0
bin==1
daemon==2
adm==3
lp==4
sync==5
shutdown==6

按字符数取列
[root@centos7 data ]#cut -c1-3 /etc/passwd
roo
bin
dae
adm
lp:
syn
shu
hal
mai

取ip地址:  先取第二行,然后取列
CentOS6
[root@CentOS6 ~ ]#ifconfig eth0 |grep -w "inet"|tr -s " " :|cut -d: -f4   ##Centos6以:作为分隔符
192.168.64.128

CentOS7:
[root@centos7 data ]#ifconfig ens33 |grep -w "inet" |tr -s " " |cut -d" " -f3
192.168.64.134

取Centos系统的主版本号:
[root@centos7 data ]#cat /etc/centos-release
CentOS Linux release 7.5.1804 (Core)
[root@CentOS6 ~ ]#cat /etc/centos-release 
CentOS release 6.10 (Final)

[root@CentOS6 ~ ]#tr -dc "[:digit:]." < /etc/centos-release |cut -d. -f1        ##除了数字和点不删,其他全部删除
6
[root@centos7 data ]#tr -dc "[:digit:]." < /etc/centos-release |cut -d. -f1
7

wc

word count的简写,
作用:统计一个文件有多少行,多少单词、多少字节、多少字符(注意:字节和字符不一样)
选项:
-l 只计数行数
-w 只计数单词总数
-c 只计数字节总数
-m 只计数字符总数
-L 显示文件中最长行的长度

默认显示行数、单词、字节数
[root@centos7 data ]#cat test 
aabbcc
[root@centos7 data ]#wc test
1 1 7 test                              ## 7个字节是因为末尾的换行符的存在
由于结果带有文件名,不方便后期做运算,可以用下面的方式仅显示数字
[root@centos7 data ]#cat test|wc 
      1       1       7

[root@centos7 data ]#cat /etc/issue
\S
Kernel \r on an \m

[root@centos7 data ]#wc -l /etc/issue
3 /etc/issue
[root@centos7 data ]#cat /etc/issue|wc -l
3
[root@centos7 data ]#cat /etc/issue|wc -w
6
[root@centos7 data ]#cat /etc/issue|wc -c
23
[root@centos7 data ]#cat /etc/issue|wc -m
23

统计当前登录用户数
[root@centos7 data ]#who
root     :0           2018-08-18 10:50 (:0)
root     pts/0        2018-08-18 10:52 (:0)
root     pts/1        2018-08-18 10:55 (192.168.64.1)
root     pts/2        2018-08-18 13:26 (192.168.64.1)
root     pts/3        2018-08-18 14:30 (192.168.64.1)
[root@centos7 data ]#who |wc -l
5

sort

作用:指定分隔符,根据第几列对文件进行排序
语法:sort [options] file(s)
选项:
-t :指定分隔符,等同于cut的-d选项
-k # :对第几列进行排序
-n :按数字大小进行排序,默认按照字母顺序排序
-r :倒序
-R : 随机排序
-u :删除输出中的重复行

取出/etc/passwd文件中的第一列和第三列,并按照数字排序
[root@centos7 data ]#cut -d: -f1,3 /etc/passwd |sort -t: -k2 -n
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
rpcuser:29
rpc:32
ntp:38

倒序
[root@centos7 data ]#cut -d: -f1,3 /etc/passwd |sort -t: -k2 -nr
nfsnobody:65534
xin:1000
polkitd:999
libstoragemgmt:998
colord:997
saslauth:996
setroubleshoot:995
chrony:994
unbound:993
gluster:992
geoclue:991
gnome-initial-setup:990

去重
[root@centos7 data ]#cat f1
aa
aa
bb
bb
cc
dd
[root@centos7 data ]#sort -u f1
aa
bb
cc
dd

随机抽取学号
[root@centos7 data ]#seq 72 |sort -R|head -n1
14
[root@centos7 data ]#seq 72 |sort -R|head -n1
40
[root@centos7 data ]#seq 72 |sort -R|head -n1
67

uniq

作用:删除相邻的重复的行
语法:uniq [OPTION]... [FILE]...

选项:
-c: 显示每行重复出现的次数
-d: 仅显示重复过的行
-u: 仅显示不曾重复的行
常和sort 命令一起配合使用:
sort userlist.txt | uniq -c

[root@centos7 data ]#cat f1
aa
aa
bb
aa
bb
bb
cc
dd
dd
[root@centos7 data ]#uniq f1           ## 默认uniq仅删除相邻的重复的行
aa
bb
aa
bb
cc
dd

[root@centos7 data ]#sort f1|uniq       ##可sort先排序,再uniq删除重复行
aa
bb
cc
dd

[root@centos7 data ]#sort f1|uniq -c    ## 统计重复出现的次数
      3 aa
      3 bb
      1 cc
      2 dd

统计一篇英语文档中每个单词出现多少次?并统计出现频率最高的前3个单词?
[root@centos7 data ]#cat f1
aa
aa
bb yy
aa www
bb
bb  zzz
cc  yy
dd  ww
dd
[root@centos7 data ]#tr -s " " "\n" < f1 |sort|uniq -c
      3 aa
      3 bb
      1 cc
      2 dd
      1 ww
      1 www
      2 yy
      1 zzz
[root@centos7 data ]#tr -s " " "\n" < f1 |sort|uniq -c|sort -nr|head -n3
      3 bb
      3 aa
      2 yy
[root@centos7 data ]#

如何取出两个文件的交集,即相同的行(保证一个文件中没有重复的行)
[root@centos7 data ]#cat f1 
aa
bb yy
aa www
bb
bb  zzz
cc  yy
dd  ww
dd
[root@centos7 data ]#cat f2
aa
bb yy
bb
cc  yy
zz
sss
[root@centos7 data ]#cat f1 f2|sort |uniq -d
aa
bb
bb yy
cc  yy
或
[root@centos7 data ]#grep -f f1 f2
aa
bb yy
bb
cc  yy

对httpd的access访问日志,判断有多少ip在访问,访问次数分别是多少?取出前10个访问量最多的ip地址?
[root@centos7 data ]#cat access_log 
192.168.32.7 - - [30/Jul/2018:10:15:34 +0800] "GET / HTTP/1.0" 403 4961 "-" "ApacheBench/2.3"
192.168.32.7 - - [30/Jul/2018:10:15:34 +0800] "GET / HTTP/1.0" 403 4961 "-" "ApacheBench/2.3"
192.168.32.7 - - [30/Jul/2018:10:15:34 +0800] "GET / HTTP/1.0" 403 4961 "-" "ApacheBench/2.3"
192.168.32.7 - - [30/Jul/2018:10:15:34 +0800] "GET / HTTP/1.0" 403 4961 "-" "ApacheBench/2.3"
192.168.32.7 - - [30/Jul/2018:10:15:34 +0800] "GET / HTTP/1.0" 403 4961 "-" "ApacheBench/2.3"
[root@centos7 data ]#cut -d" " -f1 access_log |sort |uniq -c
   2000 192.168.32.17
      5 192.168.32.5
   1100 192.168.32.7
[root@centos7 data ]#cut -d" " -f1 access_log |sort |uniq -c|sort -nr|head 
   2000 192.168.32.17
   1100 192.168.32.7
      5 192.168.32.5

diff

作用:比较两个文件有什么不同
选项:
-u

[root@centos7 data ]#cat f1 
aa
bb yy
a www
bb
bb  zzz
cc  yy
dd  ww
dd
[root@centos7 data ]#cat f2
aa
bb yy
bb
cc  yy
zz
sss
[root@centos7 data ]#diff -u f1 f2
--- f1  2018-08-18 16:45:43.484986457 +0800   ## -开头表示第一个文件
+++ f2  2018-08-18 16:31:53.353991142 +0800   ## +开头表示第二个文件
@@ -1,8 +1,6 @@                               ## 比较的范围:f1的1-8行;f2的1-6行
 aa                                           ## 前面为空表示两个文件的交集
 bb yy
-a www                                        ## 即f1多出a www,删除后两个文件相同
 bb
-bb  zzz
 cc  yy
-dd  ww
-dd
+zz                                           ## 即f2多出zz,删除后两个文件相同
+sss

建议:vimdiff f1 f2 

练习

找出ifconfig "网卡名"结果中本机的ipv4地址?

CentOS6
[root@CentOS6 ~ ]#ifconfig eth0 | grep -w "inet" |tr -s " " :|cut -d: -f4   ##Centos6以:作为分隔符
192.168.64.128

CentOS7:
[root@centos7 data ]#ifconfig ens33 | grep -w "inet" |tr -s " " |cut -d" " -f3
192.168.64.134

查出分区空间使用率的最大百分比值?

[root@centos7 data ]#df|grep "/dev/sd" |tr -s " " %|cut -d% -f5|sort -nr|head -1
16

查出用户UID最大值的用户名、UID及shel类型?

[root@centos7 data ]#cut -d: -f1,3,7 /etc/passwd|sort -t: -k2 -nr|head -1
nfsnobody:65534:/sbin/nologin

或
[root@centos7 data ]#sort -t: -k3 -nr /etc/passwd|head -1|cut -d: -f1,3,7
nfsnobody:65534:/sbin/nologin

查出/tmp的权限,以数字方式显示

[root@centos7 data ]#stat /tmp |grep "Access: (" |cut -d"(" -f2 |cut -d"/" -f1
1777
[root@centos7 data ]#stat /tmp |grep "Access: (" |cut -d"(" -f2 |head -c 4
1777[root@centos7 data ]#

统计当前连接本机的每个远程主机ip的连接数?并按照从大到小排序?

[root@centos7 data ]#netstat -nt |tr -s " " : |cut -d: -f6|sort|uniq -c|sort -nr

猜你喜欢

转载自blog.51cto.com/13668904/2161482