KandZ – Tuts

We like to help…!

Linux CLI 42 🐧 sort and uniq commands

a - sort command
sort command is used to sort text files
syntax → sort [options] [file...]
common options: -n → sorts numerically -o → specifies an output file
-f → case insensitive sorting -u → sorts and removes duplicate lines
-c → checks if file is already sorted -r → sorts in reverse order
sort -r fruit.txt → sorts text file in reverse order
sort fruit.txt → sorts text file
sort -o sorted.txt fruit.txt → sorts text file and save it to sorted.txt
ls | sort -r → using pipes, sorting the directory contents

b - uniq command
uniq command is used to report repeated lines in a sorted file
It is also used to filter out repeated lines
syntax → uniq [options] [inputfile]
common options: -c → shows number of occurrences of each line. -d → shows only repeated lines
-u → only print unique lines -s # → skip first # characters in lines
uniq fruit.txt → removing consecutive duplicates
uniq -c fruit.txt → counts occurences of lines
uniq -d fruit.txt → shows only duplicate lines
uniq -u fruit.txt → shows only unique lines

Leave a Reply