Shell日常学习之comm、sort命令

说明:由于撰写脚本的需要,研究了comm、sort命令,因为comm一般是与sort命令一起使用,先进行排序 在进行比对,结果才准确。

 sort命令:

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

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

语法

sort [-bcdfimMnr][-o<输出文件>][-t<分隔字符>][+<起始栏位>-<结束栏位>][--help][--verison][文件]

参数说明

  • -b 忽略每行前面开始出的空格字符。
  • -c 检查文件是否已经按照顺序排序。
  • -d 排序时,处理英文字母、数字及空格字符外,忽略其他的字符。
  • -f 排序时,将小写字母视为大写字母。
  • -i 排序时,除了040至176之间的ASCII字符外,忽略其他的字符。
  • -m 将几个排序好的文件进行合并。
  • -M 将前面3个字母依照月份的缩写进行排序。
  • -n 依照数值的大小排序。
  • -o<输出文件> 将排序后的结果存入指定的文件。
  • -r 以相反的顺序来排序。
  • -t<分隔字符> 指定排序时所用的栏位分隔字符。
  • +<起始栏位>-<结束栏位> 以指定的栏位来排序,范围由起始栏位到结束栏位的前一栏位。
  • --help 显示帮助。
  • --version 显示版本信息。

 

实例

在使用sort命令以默认的式对文件的行进行排序,使用的命令如下:

sort testfile 

sort 命令将以默认的方式将文本文件的第一列以ASCII 码的次序排列,并将结果输出到标准输出。

使用 cat命令显示testfile文件可知其原有的排序如下:

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

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

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

comm命令 :

 

Linux comm命令用于比较两个已排过序的文件。

这项指令会一列列地比较两个已排序文件的差异,并将其结果显示出来,如果没有指定任何参数,则会把结果分成3行显示:第1行仅是在第1个文件中出现过的列,第2行是仅在第2个文件中出现过的列,第3行则是在第1与第2个文件里都出现过的列。若给予的文件名称为"-",则comm指令会从标准输入设备读取数据。

语法

comm [-123][--help][--version][第1个文件][第2个文件]

参数

  • -1 不显示只在第1个文件里出现过的列。
  • -2 不显示只在第2个文件里出现过的列。
  • -3 不显示只在第1和第2个文件里出现过的列。
  • --help 在线帮助。
  • --version 显示版本信息。

实例

aaa.txt 与 bbb.txt 的文件内容如下:

[weblogic@hz-199-137-121 ~]$ cat aaa.txt 
aaa 
bbb 
ccc 
ddd 
eee 
111 
222
[weblogic@hz-199-137-121 ~]$ cat bbb.txt 
bbb 
ccc 
aaa 
hhh 
ttt 
jjj
[weblogic@hz-199-137-121 ~]$ comm  aaa.txt bbb.txt 
aaa 
		bbb 
		ccc 
comm: file 2 is not in sorted order
	aaa 
ddd 
eee 
comm: file 1 is not in sorted order
111 
222
	hhh 
	ttt 
	jjj
第一列  第二列  第三列

上面,大家可以看到,显然结果不准确,因为没有经过排序。

[weblogic@hz-199-137-121 ~]$ sort aaa.txt > c.txt
[weblogic@hz-199-137-121 ~]$ ll
total 16
-rw-rw-r-- 1 weblogic weblogic   34 Jun 17 14:37 aaa.txt
-rw-rw-r-- 1 weblogic weblogic   29 Jun 17 14:38 bbb.txt
drwxrwxr-x 4 weblogic weblogic 4096 Jan 17 09:36 CreateDomain
-rw-rw-r-- 1 weblogic weblogic   34 Jun 17 14:44 c.txt
[weblogic@hz-199-137-121 ~]$ cat c.txt 
111 
222
aaa 
bbb 
ccc 
ddd 
eee 
[weblogic@hz-199-137-121 ~]$ sort bbb.txt > d.txt
[weblogic@hz-199-137-121 ~]$ ll
total 20
-rw-rw-r-- 1 weblogic weblogic   34 Jun 17 14:37 aaa.txt
-rw-rw-r-- 1 weblogic weblogic   29 Jun 17 14:38 bbb.txt
-rw-rw-r-- 1 weblogic weblogic   34 Jun 17 14:44 c.txt
-rw-rw-r-- 1 weblogic weblogic   29 Jun 17 14:45 d.txt
[weblogic@hz-199-137-121 ~]$ 
[weblogic@hz-199-137-121 ~]$ 
[weblogic@hz-199-137-121 ~]$ 
[weblogic@hz-199-137-121 ~]$ 
[weblogic@hz-199-137-121 ~]$ cat d.txt 
aaa 
bbb 
ccc 
hhh 
jjj
ttt 
[weblogic@hz-199-137-121 ~]$ comm c.txt d.txt 
111 
222
		aaa 
		bbb 
		ccc 
ddd 
eee 
	hhh 
	jjj
	ttt 

经过,上面的代码,大家再看一下,结果就是我们想要的结果啦。

供参考。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Here's a possible C++ program to solve the problem: ```c++ #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream infile("SortedStrings.txt"); string prev_str, curr_str; bool sorted = true; // read the first string from the file if (getline(infile, prev_str)) { // read the rest of the strings and check if they are in ascending order while (getline(infile, curr_str)) { if (curr_str < prev_str) { sorted = false; cout << "The strings in the file are not sorted in ascending order.\n"; cout << "The first two out-of-order strings are:\n"; cout << prev_str << endl << curr_str << endl; break; } prev_str = curr_str; } } if (sorted) { cout << "The strings in the file are sorted in ascending order.\n"; } infile.close(); return 0; } ``` In this program, we first open the file "SortedStrings.txt" using an ifstream object. We then read the first string from the file using getline() function, and store it in the variable prev_str. We then start reading the rest of the strings from the file using another getline() function, and compare each string with the previous string. If we find a string that is smaller than the previous string, we set the sorted flag to false, and display the first two out-of-order strings. Otherwise, we continue reading the file until the end, and if all strings are sorted, we display a message indicating that the strings are sorted in ascending order. Note that in this program, we have used the string class to store the strings read from the file, and the < operator to compare two strings. This is possible because the string class overloads the < operator to perform lexicographic comparison of two strings.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张陈亚

您的鼓励,将是我最大的坚持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值