简体   繁体   中英

Linux shell script to sort lines of text reverse alphabetically (preferably without using sort)

I am trying to learn Linux shell scripting so am very new to this. I currently have a file (called alphabet.txt) that has 26 words, each starting with AZ but they are not ordered here. For simplicity's sake, let's just make it 3 words, all on different lines , each word starting with AC, such as the following: Banana, Apple, Carrot.

I want to know how to write a shell script that will take the contents of alphabet.txt, sort them in reverse alphabetic order and then place the sorted contents into a new file eg alphabetSorted.txt. So in the end, alphabetSorted.txt has to have the following: Apple, Banana, Carrot in order on different lines.

If possible I'd like to know how to make this work for any file and not just alphabet.txt.

你可以试试这个

sort -r alphabate.txt -o sortedAlphabet.text 

You can use sort command with '-r' flag, which specifies a reverse-order sorting.

Output can be redirect to a new file with

sort -r alphabet.txt > alphabetSorted.txt

or, using the built-in option -o, which allows the specification of an output file.

sort -r alphabet.txt -o alphabetSorted.txt

Both commands do exactly the same operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM