sort command in linux with examples | Pro Tech guides

sort command in linux with examples

sort command in linux is used to sort the content of text files in line by line. There are a lot of operations you can perform with the sort command.

sort command in linux/unix with examples | flags

sort command in linux/unix with examples
sort command in linux/unix with examples

In this session, You will learn how to use the sort command in linux. It is used to sort the content of a file. Let's see how you can use it,

Open a file in sorted format

sort command in linux/unix with examples and flags
simple sorting
$sort fileName
  • Type "sort filename" and press ENTER.
  • Your file will be sorted in alphabetical order.

sorting months

sort command in linux/unix with examples and flags
sorting months

$sort -M fileName
  • This will sort months in your file.

Numeric sorting

 
sort command in linux/unix with examples and flags
sort numbers in a file

$sort -n fileName
  • By using this "-n" flag. You can sort numbers in a file.

Random sort

sort command in linux/unix with examples and flags
Random sort

$sort -R fileName
  • To sort a file in Random in order.
  • Every time you get different output.

Reverse sorting

$sort -r fileName
  • It will reverse sort all the alphabetical order.

sort unique and eliminate duplicate content in the file

sort command in linux/unix with examples and flags
unique sorting

$sort -u fileName
  • This will eliminate duplicate lines and sort all unique content in alphabetical order.
  • But it is case sensitive i.e) capital letter is different from small letter.
  • To make this case insensitive just add the "f" flag.
$sort -uf fileName

key map sorting

sort command in linux/unix with examples and flags
key map sorting

$sort fileName -k[column].[row]
  • sort content by column and rows.
  • In the column section, you need to give the column number which you want to sort.
  • In the Row section, you need to type the corresponding column's row.
  • The sorting process is done by selected columns and rows.

sort a CSV file

sort command in linux/unix with examples and flags
sort CSV by number
$sort -t "," fileName
  • Use the above command to sort a CSV file by its number.
  • This will sort your file by numbers.
  • To sort by words.

sort command in linux/unix with examples and flags
sort CSV by names
$sort -t "," fileName -k[column].[row]
  • This will sort your CSV file by alphabet.

Check the file is sorted

sort command in linux/unix with examples and flags
check the file is sorted or not

$sort -c fileName
  • To check your file is sorted or not.
  • If the file is not sorted it will display the line where the disorder begins.
  • If the file is sorted there is no output.

Conclusion

  • "-M" sort by months.
  • "-n" sort by numbers.
  • "-R" sort randomly.
  • "-r" reverse sorting.
  • "-u" sort unique content.
  • "-f" case insensitive sorting.
  • "-k" keymap sorting.
  • "-t" input field seprator.
  • "-c" check given file is sorted or not.

So these are the most frequently used operations in the sort command in linux.

HAVE A NICE DAY💕

Related,

Name

Apps,3,Games,1,Linux,38,Linux Commands,37,Network-Devices,1,Networking,1,Terminal Apps,2,Termux,17,Userland,3,Windows,1,
ltr
item
Pro Tech guides: sort command in linux with examples
sort command in linux with examples
sort command in linux is used to sort the content of text files in line by line. There are a lot of operations you can perform with the sort command.
https://1.bp.blogspot.com/-jxG7LNrRYSc/YPfeC5GE2FI/AAAAAAAADk0/DPzfZ4dMLu0a5WF76ngWSk9ymzf5UDDXACNcBGAsYHQ/s16000/sort-command-in-linux-unix-with-examples.webp
https://1.bp.blogspot.com/-jxG7LNrRYSc/YPfeC5GE2FI/AAAAAAAADk0/DPzfZ4dMLu0a5WF76ngWSk9ymzf5UDDXACNcBGAsYHQ/s72-c/sort-command-in-linux-unix-with-examples.webp
Pro Tech guides
http://www.protechguidez.com/2021/10/sort-command-in-linux-with-examples.html
http://www.protechguidez.com/
http://www.protechguidez.com/
http://www.protechguidez.com/2021/10/sort-command-in-linux-with-examples.html
true
7053638852026728047
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content