当前位置:网站首页>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
边栏推荐
- Unity Editor Extension - Outline
- 基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
- [concurrent programming] synchronization container, concurrent container, blocking queue, double ended queue and work secret
- Annotations simplify configuration and loading at startup
- C language student management system based on linked list, super detailed
- JS non Boolean operation - learning notes
- Notes and bugs generated during the use of h:i:s and y-m-d
- Deep parsing JVM memory model
- Complex character + number pyramid
- How to use Jupiter notebook
猜你喜欢
Unity editor expansion - draw lines
Unity editor expansion - controls, layouts
数位统计DP AcWing 338. 计数问题
[concurrent programming] concurrent tool class of thread
Notes and bugs generated during the use of h:i:s and y-m-d
Concurrent programming (VI) ABA problems and solutions under CAS
【Rust笔记】02-所有权
Sending and receiving of request parameters
How to use Jupiter notebook
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
随机推荐
Location of package cache downloaded by unity packagemanager
Markdown learning
Deep parsing (picture and text) JVM garbage collector (II)
TP5 order multi condition sort
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
createjs easeljs
[rust notes] 06 package and module
Get the link behind? Parameter value after question mark
How to deal with the core task delay caused by insufficient data warehouse resources
Phpstudy 80 port occupied W10 system
JS non Boolean operation - learning notes
Convert video to GIF
Complex character + number pyramid
TP5 multi condition sorting
Unity editor expansion - scrolling list
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
Servlet的生命周期
Concurrent programming (III) detailed explanation of synchronized keyword
Arbre DP acwing 285. Un bal sans patron.
UE4 source code reading_ Bone model and animation system_ Animation node