Tips to Use Sort Command to Sort Text Files in Linux

Tips to Use Sort Command to Sort Text Files in Linux

Hello Geeky, so today we are focusing on How to Use Sort Command to Sort Text Files in Linux. So please read this tutorial carefully so you may comprehend it in a better helpful way.

Guide: How to Use Sort Command to Sort Text Files in Linux

The sort command can be used to sort and organize lines in a text file. You have the option of selecting data in a text file and displaying the results on the screen, or directing it to a file. Sort of provides multiple command options for selecting data in a text file, depending on your needs.

Sort Syntax Command:

$ respectively [-options]

For example, this is a test file:

$ helps test zzz sss qqq aaa BBB ddd AAA

And, this is what you get when ordering works on this file without any option. Arrange the lines in the test file and specify the production sequence.

$ too test aaa AAA BBB ddd qqq sss zzz

1. Make a Number sort using the -n option

If we want to sort by numeric value, then we can use the -n or –numeric-sort option.

Create the following test file for this example:

Test $ cats 22 zzz 33 sss 11qqq 77 aaa 55 BBB

The following sort order commands the lines in the test file on the numeric value in the first word of the line and introduces the sequence.

$ sort -n test 11qqq 22 zzz 33 sss 55 BBB 77 aaa

2. Sort Human Readable Numbers using -h option

If we want to sort on human read numbers (for example, 2K 1M 1G), then we can use the -h or -man-numeric- too.

Create the following test file for this example:

$ helps test 2K 2G 1K 6T 1T 1G 2M

The following true order sets the number of people (ie 1K = 1 Thousand, 1M = 1 Million, 1G = 1 High, 1T = 1 Tera) in the test file and displays the production sequence.

$ too -h test 1K 2K 2M 1G 2G 1T 6T

3. Sort the months of a year using the -M option

If we want to sort in the order of the months of the year, then we can use the -M-or-months sort.

Create the following test file for this example:

$ cat test Sept aug Jan Oct apr February Mar11

The following true command lists the lines in the test file as a monthly command. Note, the lines in the file should contain at least three characters of the month name at the beginning of the line (e.g. Jan, Feb, Mar). If we give, fight for January or au for August, then sort command will not consider it as month name.

$ too -M test Jan feb Mar11 apr aug September October

4. Check whether the content is sorted using the -c option

If we want to check the data in the text file is sequential or not, then we can use -c or –check, –check = diagnose-first option.

Create the following test file for this example:

Test $ cats 2 5 1 6

The following command list checks whether text file data is sorted or not. If not, then it shows the first event with line number and chaotic value.

$ sort -c type of test: test: 3: chaos: 1

5. Change the Exit and Check for Uniqueness using -r and -u options

If we want to sort the output in reverse order, then we can use the -r or -reverse option. If the file contains duplicate lines, then to get the duplicate lines in direct production, the “-u” option can be used.

Create the following test file for this example:

Test $ cats 5 2 2 1 4 4

The following order sort the lines in the test file in reverse order and specify the output sequence.

$ too -r test 5 4 4 2 2 1

The following sort of command sort the lines in the test file in a reverse way and remove the duplicate lines from the sequencing.

$ sort -r -u test 5 4 2 1

6. Selecting the Content, Customize Flat, Write the output to the file using -k, -t, -o options

If we want to sort on the page or text position in the lines of the text file, then the “-k” option can be used. If each word in each file line is separated by a partition except ‘field’, then the partition can be specified using the “-t” option. Sort output can be obtained in any specific output file (using the “-o” option) instead of the output output on standard output.

Create the following test file for this example:

Cats test aa aa zz aa aa ff aa aa tt aa aa kk

The next sort of command is the lines in the test file on the 3rd word of each line and introduces the sequence.

$ too -k3 test aa aa ff aa aa kk aa aa tt aa aa zz $ test cats aa | 5a | zz aa | 2a | ff aa | 1a | tt aa | 3a | kk

Here, several options are used together. In the test file, words in each line is separated by delimiter ‘|’. Arrange the lines in the test file on the second word of each line on the basis of the numerical value and the stores sorted into a specific output file.

$ respectively -n -t ‘|’ -k2 test -o outfile

The output file content is shown below.

$ cats outfile aa | 1a | tt aa | 2a | ff aa | 3a | kk aa | 5a | zz

Faq

Guide about How to Use Sort Command to Sort Text Files in Linux


In this guide, we told you about the How to Use Sort Command to Sort Text Files in Linux; please read all steps above so that you understand How to Use Sort Command to Sort Text Files in Linux in case if you need any assistance from us, then contact us.

How this tutorial or guide assisting you?


So in this guide, we discuss the How to Use Sort Command to Sort Text Files in Linux, which undoubtedly benefits you.



Final note

I hope you like the guide How to Use Sort Command to Sort Text Files in Linux. In case if you have any queries regards this article/tutorial you may ask us. Also, please share your love by sharing this article with your friends and family.

Trickbugs: Get the latest Trick and Tips
Logo
Enable registration in settings - general