当前位置:网站首页>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
边栏推荐
- On the difference and connection between find and select in TP5 framework
- Arbre DP acwing 285. Un bal sans patron.
- On the setting of global variable position in C language
- Log4j2 vulnerability recurrence and analysis
- ES6 promise learning notes
- 【Rust 笔记】07-结构体
- Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
- Monotonic stack -42 Connect rainwater
- Query XML documents with XPath
- How to deal with the core task delay caused by insufficient data warehouse resources
猜你喜欢

二进制转十进制,十进制转二进制

Notes and bugs generated during the use of h:i:s and y-m-d

DOM 渲染系统(render mount patch)响应式系统

Unity interactive water ripple post-treatment

Deeply understand the underlying data structure of MySQL index

Es8 async and await learning notes

【Rust笔记】02-所有权

Alibaba canal actual combat
![[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle](/img/b7/7bf2a4a9ab51364352aa5e0a196b6d.jpg)
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle

Notes on understanding applets 2022/7/3
随机推荐
[rust notes] 05 error handling
PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
Unity Editor Extension - drag and drop
Collection interface
【Rust 笔记】10-操作符重载
高斯消元 AcWing 883. 高斯消元解线性方程组
[rust notes] 07 structure
JS ternary operator - learning notes (with cases)
How to use Jupiter notebook
[concurrent programming] consistency hash
Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
Unity editor expansion - draw lines
22-05-26 西安 面试题(01)准备
php public private protected
Monotonic stack -503 Next bigger Element II
状态压缩DP AcWing 291. 蒙德里安的梦想
createjs easeljs
Unity notes 1
Graphics_ Games101/202 learning notes
Redux - learning notes