Linux文本处理相关命令

  • A+
所属分类:linux技术
摘要

Linux sort命令用于将文本文件内容加以排序。sort 可针对文本文件的内容,以行为单位来排序。


一、文本处理命令

1.1 sort命令

Linux sort命令用于将文本文件内容加以排序。

sort 可针对文本文件的内容,以行为单位来排序。

语法格式如下:

sort [参数]...[文件] 
相关参数 参数说明
-n 依照数值的大小排序
-r 以相反的顺序来排序
-k 以某列进行排序
-t 指定分割符,默认是以空格为分隔符

案例:

$ cat testfile      # testfile文件原有排序   test 30   Hello 95   Linux 85  

使用sort命令重新排序后的结果如如下:

$ sort testfile # 重排结果   Hello 95   Linux 85   test 30  

使用 -k 参数设置对第二列的值进行重排,结果如下:

$ sort testfile -k 2 test 30   Linux 85  Hello 95 

1.2 uniq命令

Linux uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用。

uniq 可检查文本文件中重复出现的行列。

语法格式如下:

uniq [-cdu]...[输入文件][输出文件] 
相关参数 参数说明
-c 在每列旁边显示该行重复出现的次数
-d 仅显示重复出现的行列
-u 仅显示出一次的行列
[输入文件] 指定已排序好的文本文件。如果不指定此项,则从标准读取数据
[输出文件] 指定输出的文件。如果不指定此选项,则将内容显示到标准输出设备(显示终端)

案例:

testfile中的原有内容为:

$ cat testfile      #原有内容   test 30   test 30   test 30   Hello 95   Hello 95   Hello 95   Hello 95   Linux 85   Linux 85  

使用uniq 命令删除重复的行后,有如下输出结果:

$ uniq testfile     #删除重复行后的内容   test 30   Hello 95   Linux 85  $ uniq -c testfile      #删除重复行后的内容   3 test 30             #前面的数字的意义为该行共出现了3次   4 Hello 95            #前面的数字的意义为该行共出现了4次   2 Linux 85            #前面的数字的意义为该行共出现了2次  

当重复的行并不相邻时,uniq 命令是不起作用的,即若文件内容为以下时,uniq 命令不起作用:

$ cat testfile1      # 原有内容  test 30   Hello 95   Linux 85  test 30   Hello 95   Linux 85  test 30   Hello 95   Linux 85  

这时我们就可以使用 sort:

$ sort  testfile1 | uniq Hello 95   Linux 85  test 30 

统计各行在文件中出现的次数:

$ sort testfile1 | uniq -c    3 Hello 95      3 Linux 85     3 test 30 

在文件中找出重复的行:

$ sort testfile1 | uniq -d Hello 95   Linux 85  test 30  

1.3 cut命令

Linux cut命令用于显示每行从开头算起 num1 到 num2 的文字。

语法格式如下:

cut [-c]  cut [-df]  ... 

使用说明:

cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段写至标准输出。

如果不指定 File 参数,cut 命令将读取标准输入。必须指定 -b、-c 或 -f 标志之一。

相关参数 参数说明
-c 以字符为单位进行分割
-d 自定义分隔符,默认为制表符
-f 与-d一起使用,指定显示哪个区域

案例:

[root@jiangzhi ~]# cat juechen  # 原文件内容 |7|9|8|7|5|5|3|4|5|1|3|4|5 |7|8|7|8|5|2|5|4|5|1 |6|5|7|6|7|6|5|3|2 |6|5|6|7|6|5|7 |3|2|4|3|4|5|2|3|4|6|2|5 |2|3|3|5|3|4|6 |2|1|5|3|4|5|3|2|4 |5|4|5|4|3 |2|5|3|4|5|2|3 

如果我们想提取每一行的第2个字节,可以这样:

[root@jiangzhi ~]# cat juechen | cut -b2 7 7 6 6 3 2 2 5 2 

1.4 tr命令

Linux tr 命令用于转换或删除文件中的字符。

tr 指令从标准输入设备读取数据,经过字符串转译后,将结果输出到标准输出设备。

语法格式如下:

tr [-cdst]...[第一字符集][第二字符集]   tr [OPTION]…SET1[SET2]  
相关参数 参数说明
-c, --complement 反选设定字符。也就是符合 SET1 的部份不做处理,不符合的剩余部份才进行转换
-d, --delete 删除指令字符
-s, --squeeze-repeats 缩减连续重复的字符成指定的单个字符
-t, --truncate-set1 削减 SET1 指定范围,使之与 SET2 设定长度相等

案例:

testfile文件中的内容如下:

$ cat testfile         #testfile原来的内容   Linux networks are becoming more and more common,  but scurity is often an overlooked   issue. Unfortunately, in today’s environment all networks  are potential hacker targets,   fro0m tp-secret military research networks to small home LANs.   Linux Network Securty focuses on securing Linux in a  networked environment, where the   security of the entire network needs to be considered rather than just isolated machines.   It uses a mix of theory and practicl techniques to  teach administrators how to install and   use security applications, as well as how the  applcations work and why they are necesary.  

将文件testfile中的小写字母全部转换成大写字母,此时,可使用如下命令:

cat testfile |tr a-z A-Z 

使用 tr 命令大小写转换后,得到如下输出结果:

$ cat testfile | tr a-z A-Z #转换后的输出   LINUX NETWORKS ARE BECOMING MORE AND MORE COMMON, BUT SCURITY IS OFTEN AN OVERLOOKED  ISSUE. UNFORTUNATELY, IN TODAY’S ENVIRONMENT ALL NETWORKS ARE POTENTIAL HACKER TARGETS,   FROM TP-SECRET MILITARY RESEARCH NETWORKS TO SMALL HOME LANS.   LINUX NETWORK SECURTY FOCUSES ON SECURING LINUX IN A NETWORKED ENVIRONMENT, WHERE THE  SECURITY OF THE ENTIRE NETWORK NEEDS TO BE CONSIDERED RATHER THAN JUST ISOLATED MACHINES.   IT USES A MIX OF THEORY AND PRACTICL TECHNIQUES TO TEACH ADMINISTRATORS HOW TO INSTALL AND USE SECURITY APPLICATIONS, AS WELL AS HOW THE APPLCATIONS WORK AND WHY THEY ARE NECESARY. 

1.5 wc命令

Linux wc命令用于计算字数。

利用wc指令我们可以计算文件的Byte数、字数、或是行数,若不指定文件名称、或是所给予的文件名为"-",则wc指令会从标准输入设备读取数据。

语法格式如下:

wc [-clw]...[文件...] 
相关参数 参数说明
-c 统计文件的Bytes数
-l 统计文件的行数
-w 统计文件中单词的个数,默认以空白字符做为分隔符

注:在Linux系统中,一段连续的数字或字母组合为一个词

实例:

在默认的情况下,wc将计算指定文件的行数、字数,以及字节数。先查看testfile文件的内容,可以看到:

$ cat testfile   Linux networks are becoming more and more common, but scurity is often an overlooked  issue. Unfortunately, in today’s environment all networks are potential hacker targets,fro0m tp-secret military research networks to small home LANs.   Linux Network Securty focuses on securing Linux in a networked environment, where the  security of the entire network needs to be considered rather than just isolated machines.   It uses a mix of theory and practicl techniques to teach administrators how to install and use security applications, as well as how the applcations work and why they are necesary.  

使用wc统计,结果如下:

$ wc testfile           # testfile文件的统计信息   3 91 601 testfile       # testfile文件的行数为3、单词数91、字节数601  

其中,3 个数字分别表示testfile文件的行数、单词数,以及该文件的字节数。

如果想同时统计多个文件的信息,例如同时统计testfile、testfile_1、testfile_2,可使用如下命令:

wc testfile testfile_1 testfile_2   #统计三个文件的信息  

输出结果如下:

$ wc testfile testfile_1 testfile_2  #统计三个文件的信息   3 91 601 testfile                 # testfile文件的行数为3、单词数91、字节数601   9 18 78 testfile_1                   #第二个文件的行数为9、单词数18、字节数78   3 6 32 testfile_2                    #第三个文件的行数为3、单词数6、字节数32   15 115 711 总用量                    #三个文件总共的行数为15、单词数115、字节数711