Linux文本的处理

Linux最最最最重要的哲学思想就是:一切皆文件。文件以及文件的操作在LInux操作系统中是非常的重要。熟练使用精悍小巧快捷的文本处理方式让效率更高。

一、文件查看命令

less 分页查看

[root@centos7 app]# cat /etc/passwd |less

cat

空行指的是没有任何字符,如果一行中只有空格那也不能称为空行。

  • 对每一行进行编号,不论一行有没有内容都显示行号。
[root@centos7 app]# cat -n /etc/passwd
  • 对非空行进行编号
    如果一行当中只有空格,那么这一行也不是非空行。使用-b时也进行编号
[root@centos7 app]# cat -b file
  • 压缩空行
    压缩连续的空行为一行,一行中只有空格那也不是空行
[root@centos7 app]# cat -s file
  • 在每一行结束地添加$
[root@centos7 app]# cat -E $ /etc/passwd

二、分页查看内容

  • 分页查看more
    能显示文件剩余的百分比
[root@centos7 app]# more passwd 
[root@centos7 app]# cat passwd | more 
  • 分页查看less
[root@centos7 app]# cat /etc/profile.d/colorls.sh  | less

less可以使用搜索使用 /string搜索字符串。
n或者N跳转到下一个或者上一个匹配。


三、截取文本的前行或者后行

  • head
    截取指定前几行或者前几字节的内容
[root@centos7 app]# cat colorls.sh | head -c 10            #显示指定前10字节
[root@centos7 app]# cat colorls.sh | head -n 10            #显示指定前10行
[root@centos7 app]# cat colorls.sh | head -10
  • tail
    tail与head正好相反,作用是截取文本的后几行或后几字节。
    tail与head常常搭配使用截取除指定的行
[root@centos7 app]# cat clorls.sh | tail -c 10             #显示指定后10字节
[root@centos7 app]# cat clorls.sh | tail -n 10             #显示指定后10行
[root@centos7 app]# cat clorls.sh | tail -10               #显示指定后10行
  • head与tail搭配使用,精确找出第三行
[root@centos7 app]# cat file|head -3|tail -1

四、截取文本的某一列

  • cut
    cut可以指定分隔符后,截取出指定的列。如果一行没有分隔符的话,整行输出。
 #指定冒号为分隔符,只显示第2列
[root@centos7 app]# cat file |cut -d: -f2       
 #指定冒号为分隔符,显示2到3列        
[root@centos7 app]# cat file |cut -d: -c2-3             
#
#如果分隔符有歧义,那么需要加引号
[root@centos7 app]# cat file |cut -d";" -f2 
  • paste
    将两个文本同行号的内容,输出到一行
默认使用tab做分隔符
[root@centos7 app]# paste file file2
1       a
1       a
1       a
#指定冒号作文分隔符
[root@centos7 app]# paste -d: file file2
#将所有内容输出到一行
[root@centos7 app]# paste -s file file2
1       1       1
a       a       a

五、文本统计工具与排序

  • wc可以用来统计行数,字数,字符数
[root@centos7 app]# wc passwd 
  42     88    2308    passwd
 行数  文字数   字符数   文件名
#
#统计行数
[root@centos7 app]# wc passwd -l
42 passwd
#统计字符数
[root@centos7 app]# wc passwd -w
88 passwd
#统计字节数
[root@centos7 app]# wc passwd -c
2308 passwd
  • sort工具用来对文本进行排序
#删除重复的行
[root@centos7 app]# sort -u file2
#按字数的大小进行排序
[root@centos7 app]# sort -n file2
#    -r 执行反方向(由上至下)整理
#    -n 执行按数字大小整理
#    -f 选项忽略(fold)字符串中的字符大小写
#    -u 选项(独特,unique)删除输出中的重复行
#    -t c 选项使用c做为字段界定符
#    -k X 选项按照使用c字符分隔的X列来整理能够使用多次
  • uniq 删除连续重复的行为一行
压缩连续重复的行为一行
[root@centos7 app]# uniq file2
#统计每行重复出现的次数
[root@centos7 app]# uniq file2 -c
#显示不曾连续重复的行
[root@centos7 app]# uniq file2 -u

练习题

1、找出ifconfig “网卡名” 命令结果中本机的IPv4地址

[root@centos7 app]# ifconfig ens33 |head -2|tail -1|tr -s ' '|cut -d ' ' -f3
192.168.0.129

解析

ifconfig ens3--------显示网卡信息
head -2|tail -1------保留IP信息的一行
tr -s ' ' -----------将空格压缩
cut -d ' ' -f3-------空格为分隔符显示出第三列

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

[root@centos7 app]# df -h| tr -s ' '|cut -d ' ' -f5
Use%
7%
0%
1%

解析

tr -s ' '--------------压缩空格
cut -d ' ' -f5---------以空格为分隔符,截取第五列

3、查出用户UID最大值的用户名、UID及shell类型

[root@centos7 app]# cut -d: -f1,3,7 /etc/passwd|sort -t : -k 2 -n|tail -1
nfsnobody:65534:/sbin/nologin

解析

cut -d: -f1,3 /etc/passwd-----以:为分隔符,取1,3列
sort -t : -k 2 -n-------------以:作为分隔符,按第二列从小到大排序
tail -1-----------------------排序后最后一行是最大的,所以取最后一行

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

#用命令本身的快捷方法
[root@centos7 app]# stat -c %a /tmp/
1777
#用本章的知识解决
[root@centos7 app]# stat /tmp/ | head -n4|tail -1|tr '(' '/'|cut -d/ -f2
1777

解析

stat /tmp/ -----------------此命令可以查看到目录数字形式的权限
head -n4--------------------数字权限在第四行,所以取出前四行
tail -1---------------------取出只有数字权限的一行
|tr '(' '/'-----------------设置分隔符
cut -d/ -f2-----------------取出权限

原文地址:https://www.cnblogs.com/aubin/p/7242383.html