- Linux Shell Scripting Cookbook(Third Edition)
- Clif Flynt Sarath Lakshman Shantanu Tushar
- 153字
- 2021-07-09 19:46:20
How to do it...
- We can sort a set of files (for example, file1.txt and file2.txt), like this:
$ sort file1.txt file2.txt > sorted.txt
Alternatively, use this:
$ sort file1.txt file2.txt -o sorted.txt
- For a numerical sort, we use this:
$ sort -n file.txt
- To sort in the reverse order, we use the following command:
$ sort -r file.txt
- To sort by months (in the order Jan, Feb, March,...), use this:
$ sort -M months.txt
- To merge two already sorted files, use this command:
$ sort -m sorted1 sorted2
- To find the unique lines from a sorted file, use this:
$ sort file1.txt file2.txt | uniq
- To check whether a file has already been sorted, use the following code:
#!/bin/bash #Desc: Sort sort -C filename ; if [ $? -eq 0 ]; then echo Sorted; else echo Unsorted; fi
Replace filename with the file you want to check and run the script.