🖥️sort

➡️This is a command-line reference manual for commands and command combinations that you don’t use often enough to remember it. This cheatsheet explains the sort command with important options and switches using examples.

▁ ▂ ▃ ▄ ꧁ 🔴☠ COMMANDLINE-KUNGFU WITH CHEATSHEETS ☠🔴꧂▅ ▃ ▂ ▁

#                ███████╗ ██████╗ ██████╗ ████████╗
#                ██╔════╝██╔═══██╗██╔══██╗╚══██╔══╝
#                ███████╗██║   ██║██████╔╝   ██║   
#                ╚════██║██║   ██║██╔══██╗   ██║   
#                ███████║╚██████╔╝██║  ██║   ██║   
#                ╚══════╝ ╚═════╝ ╚═╝  ╚═╝   ╚═╝   
                                                  
                                                 
# To sort a file:
sort file

# To sort a file by keeping only unique:
sort -u file

# To sort a file and reverse the result:
sort -r file

# To sort a file randomly:
sort -R file

#==============================#
## CMD SORT 
##==============================##==============================#
sort +0d -1 +1n -2 file.txt
#

sort -c
# does not sort but checks whether a file is sorted, reporting first exception if not sorted.

sort -f
# Sort a file in case-insensitive order: sort -f. 'f' for 'fold,' i.e. fold upper and lower case together

sort -k 3
# sort a file based on the 3rd field in each line: sort -k 3. Fields separated by white space.

sort -k4 *.log |sed -n '/15\/Aug\/2015:14:11:02/,/20\/Aug\/2015:02:34:58/p' |cut -d" " -f1|sort|uniq|wc -l
# Count uniq hosts in time range.

sort -n
# Sort a file numerically

sort -u
# So common that most sort commands have a -u option to save you the trouble of piping to uniq

sort a file by the first column in dictionary order, then by the second column numerically.
# Multiple column sort

sort gkwzFruP.txt | awk '{if(l1==$1 && l3!=$3){print ll "\n" $0}; ll=$0;l1=$1;l3=$3}'
# Print dupes where the 3rd column changed. 

sort popular.txt | uniq -cd
# Right. You will want to sort the input to uniq first. 

sort -f. 'f' for 'fold,' i.e. fold upper and lower case together
# Sort a file in case-insensitive order

sort -t, -k5nr data.csv | less 
# Sort data.csv by the 5th column's numeric values in descending order.

sort -u roster1.txt roster2.txt | wc -l 
# Given two overlapping rosters of normalized names, count the total number of unique names across both rosters.

sort -g /var/log/nginx/access.log | awk '{print $1}' | uniq 
# Show all IPs that accesed your nginx server. 

sort -V ipv4addrs.txt
# In GNU sort, you can use -V (version sort) to also sort IPv4 addresses numerically according to each class.

sort -V ipv4addrs.txt 
# In GNU sort, you can use -V (version sort) to also sort IPv4 addresses numerically according to each octet. For IPv6, try using ipv6calc --addr2fulluncompaddr first to normalize the addresses and pass to a plain sort.

# Sort by IP address
sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4

# Sort two lists of names by the 2nd column (last name) so that you can compare the [hopefully] similar entries for matches. I just used this (after some normalizing) to make sure that I had a complete list of names in a document.
sort -k2 names1.txt names2.txt | less 

# sort list of email addresses by domain.tld email random list can be created here: https://www.randomlists.com/email-addresses 
sort -t@ -k2 emails.txt

# Sort and remove duplicate lines from two (or more files). Display only uniq lines from files.
sort file1 file2 | uniq -u
# Explanation: The -u flag of uniq removes duplicate lines from the input.
# Example file1:
	123456
	234567
	345678
# Example file2:
	234567
	345678
	456789
# Result:
	123456
	456789

#==============================##==============================#
## CMD SORT 
##==============================##==============================#
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

  █║▌│║█║▌★ KALI ★ PARROT ★ DEBIAN 🔴 PENTESTING ★ HACKING ★ █║▌│║█║▌

              ██╗ ██╗ ██████╗  ██████╗ ██╗  ██╗███████╗██████╗
             ████████╗██╔══██╗██╔═══██╗╚██╗██╔╝██╔════╝██╔══██╗
             ╚██╔═██╔╝██║  ██║██║   ██║ ╚███╔╝ █████╗  ██║  ██║
             ████████╗██║  ██║██║   ██║ ██╔██╗ ██╔══╝  ██║  ██║
             ╚██╔═██╔╝██████╔╝╚██████╔╝██╔╝ ██╗███████╗██████╔╝
              ╚═╝ ╚═╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚══════╝╚═════╝

               █║▌│║█║▌ WITH COMMANDLINE-KUNGFU POWER █║▌│║█║▌

░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░