当前位置:网站首页>Use of sort command in shell
Use of sort command in shell
2022-07-03 08:53:00 【Xiaoping is extraordinary】
One 、sort How commands work
sort The command takes each line of the file as the comparison object , By comparing different peers , So as to get the final result . The comparison principle is to start with the first character , Press backward in sequence ASCII Code value for comparison , Finally, output the results in ascending order
Compare the previous results :
[[email protected] programming]$ cat seq.txt
banana
apple
pear
orange
The result after comparison :
[[email protected] programming]$ sort seq.txt
apple
banana
orange
pear
Two 、sort Command parameters
2.1 sort -u
The function is to remove duplicate lines in the output line
Use only sort Result :
[[email protected] programming]$ sort seq.txt
apple
banana
orange
pear
pear
add -u Result :
[[email protected] programming]$ sort -u seq.txt
apple
banana
orange
pear
2.2 sort -r
Role is ,sort The default sorting method of the command is ascending , If you want to change it to descending order , You need to add -r
2.3 sort -o
because sort The default is to output the results to standard output , So redirection is needed to convert the result write file , Form like sort filename > newfile. however , If you want to output the sorting results to In the original document , Not with redirection
As shown below : This situation will number.txt Empty
[[email protected] programming]$ sort -r number.txt > number.txt
[[email protected] programming]$ cat number.txt
[[email protected] programming]$
Right now ,-o The option appears , It successfully solved this problem , Let you feel free to write the results to the original file
[[email protected] programming]$ cat number.txt
1
3
5
2
4
After using :
[[email protected] programming]$ sort -r number.txt -o number.txt
[[email protected] programming]$ cat number.txt
5
4
3
2
1
2.4 sort -t And -k
If there is a file like this :
[[email protected] programming]$ cat facebook.txt
banana:30:5.5
apple:10:2.5
pear:90:2.3
orange:20:3.4
This file has three columns , Columns are separated by colons , The first column shows the type of fruit , The second column shows the number of fruits , The third column shows the price of fruit
So I want to sort by the number of fruits , That is, sort by the second column , How to use it sort Realization ?
fortunately ,sort Provides -t Options , You can set a space character later . After specifying the spacer , You can use it -k To specify the number of columns
[[email protected] programming]$ sort -n -k 2 -t : facebook.txt
apple:10:2.5
orange:20:3.4
banana:30:5.5
pear:90:2.3
We use a colon as a separator , And sort the second column in numerical ascending order , The results are very satisfactory
2.5 Other sort Common options
- -f Will convert all lowercase letters to uppercase letters for comparison , That is, ignore case
- -c Will check if the file is in order , If in disorder , Then output the information about the first disordered row , Finally back to 1
- -C Will check if the file is in order , If in disorder , No output , Return only 1
- -M It will be sorted by month , such as JAN Less than FEB wait
- -b All blanks in front of each line are ignored , Compare from the first visible character
边栏推荐
- [concurrent programming] explicit lock and AQS
- 记忆化搜索 AcWing 901. 滑雪
- UE4 source code reading_ Mobile synchronization
- Redux - learning notes
- Log4j2 vulnerability recurrence and analysis
- MySQL three logs
- How to deal with the core task delay caused by insufficient data warehouse resources
- [concurrent programming] synchronization container, concurrent container, blocking queue, double ended queue and work secret
- 22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
- Eating fruit
猜你喜欢

树形DP AcWing 285. 没有上司的舞会

Query XML documents with XPath

Unity interactive water ripple post-treatment

Es8 async and await learning notes

Unity editor expansion - controls, layouts

Concurrent programming (III) detailed explanation of synchronized keyword

SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time

Sending and receiving of request parameters

XPath实现XML文档的查询

Gif remove blank frame frame number adjustment
随机推荐
Es8 async and await learning notes
Format - C language project sub file
Markdown learning
[rust notes] 13 iterator (Part 1)
C language file reading and writing
[linear table] basic operation of bidirectional linked list specify node exchange
On the setting of global variable position in C language
Monotonic stack -84 The largest rectangle in the histogram
URL backup 1
Really explain the five data structures of redis
Get the link behind? Parameter value after question mark
Annotations simplify configuration and loading at startup
Binary tree sorting (C language, int type)
22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
[rust notes] 06 package and module
求组合数 AcWing 886. 求组合数 II
状态压缩DP AcWing 291. 蒙德里安的梦想
20220630学习打卡
How to deal with the core task delay caused by insufficient data warehouse resources
The method for win10 system to enter the control panel is as follows: