Sort by bash a text file by the first string (till

2019-09-12 10:02发布

I have a text file for acronyms. Each line starts with the acronym (in uppercase), usually up to 4 letters, and then "=" and then the explanation. For example,

...

EST=Eastern Standard Time
OVS=Open vSwitch
IPMI=Intelligent Platform Management Interface 

...

IHV=Independent Hardware Vendor    
ISV=Independent Software Vendor

...

I want to sort this text file, in bash, by ascending order according to the acronym, which is the first string (till "=") in each line. Any suggestions as to the best way to achieve this ?

标签: linux bash shell
1条回答
萌系小妹纸
2楼-- · 2019-09-12 10:53

Take a look at the sort command (man sort). It has all manner of options for delimiting keys and establishing sorting parameters.

In your simple case:

sort --field-separator="=" < sort.list

would set the primary key to be everything up to the = and sort them in ascending order.

There is a "short form" for this, also (thanks @karafka!):

sort -t =

Check out man sort for details on sort order, using multiple keys and other options.

查看更多
登录 后发表回答