所以我有一个名为 的文件test.txt
,其中包含以下几行。
1.The Home Depot (Ranked 28): home improvement; Atlanta, GA.
2.United Parcel Service (Ranked 48): shipping; Atlanta, GA.
3.The Coca-Cola Company (Ranked 62): soft drinks; Atlanta, GA.
4.Delta Air Lines, Inc. (Ranked 68): airline; Atlanta, GA.
5.Aflac, Incorporated (Ranked 135): insurance; Columbus, GA.
6.The Southern Company (Ranked 162): energy; Atlanta, GA.
7.Genuine Parts Company (Ranked 183): replacement parts; Atlanta, GA.
8.First Data Corporation (Ranked 249): payment processing;Atlanta,GA.
9.WestRock (Ranked 251): corrugated and consumer Norcross, GA
10.HD Supply Holdings, Inc. (Ranked 320): industrial , Atlanta GA
11.Suntrust Banks, Inc. (Ranked 329): banking; Atlanta, GA.
12.Mohawk Industries, Inc. (Ranked 338): flooring manufacturer; Calhoun, GA.
我使用的命令sed 's/[0-9]*[.]//' test.txt | awk -F"(" '{print NF,$1}' | sort
仅按字母顺序给出公司名称。
Aflac, Incorporated
Delta Air Lines
Genuine Parts Company
First Data Corporation
HD Supply Holdings
Mohawk Industries
Suntrust Banks
The Coca-Cola Company
The Home Depot
The Southern Company
不过,我希望也显示整行,而不仅仅是公司名称。
答案1
(取2)假设文件内容为:
1.The Home Depot (Ranked 28): home improvement; Atlanta, GA.
2.United Parcel Service (Ranked 48): shipping; Atlanta, GA.
3.The Coca-Cola Company (Ranked 62): soft drinks; Atlanta, GA.
4.Delta Air Lines, Inc. (Ranked 68): airline; Atlanta, GA.
由于要排序的数据出现在点之后,因此您可以显式地将点设置为分隔符-t .
,然后对第二个字段进行排序:
$ sort -t. -k2 test.txt
4.Delta Air Lines, Inc. (Ranked 68): airline; Atlanta, GA.
3.The Coca-Cola Company (Ranked 62): soft drinks; Atlanta, GA.
1.The Home Depot (Ranked 28): home improvement; Atlanta, GA.
2.United Parcel Service (Ranked 48): shipping; Atlanta, GA.
(问题中显示了一些前导空格。我只是将其复制到此处,但无论是否真的存在,对于排序并不重要。)