Monday 21 December 2020

Top 10 example of sort command in UNIX or Linux

Sorting is an essential task when working with data, especially large datasets. In Unix and Linux systems, the 'sort' command is used to sort files or data streams. In this blog article, we will explore ten examples of the 'sort' command that are frequently used in Unix and Linux systems.

Example 1: Sort a File Alphabetically

To sort a file alphabetically, you can use the following command:

sort file.txt

This command will sort the contents of 'file.txt' in alphabetical order and print the result on the console.

Example 2: Sort a File in Reverse Order

If you want to sort a file in reverse order, you can use the following command:

sort -r file.txt

This command will sort the contents of 'file.txt' in reverse order and print the result on the console.

Example 3: Sort a File Numerically

If you have a file that contains numerical values, you can sort it numerically using the following command:

sort -n file.txt

This command will sort the contents of 'file.txt' numerically and print the result on the console.

Example 4: Sort a File Based on Column

If you have a file with multiple columns and you want to sort it based on a particular column, you can use the following command:

sort -k 2 file.txt

This command will sort the contents of 'file.txt' based on the second column and print the result on the console.

Example 5: Sort a File Based on Multiple Columns

If you have a file with multiple columns and you want to sort it based on multiple columns, you can use the following command:

sort -k 2,3 file.txt

This command will sort the contents of 'file.txt' based on the second and third columns and print the result on the console.

Example 6: Sort a File in Unique Mode

If you want to sort a file and remove duplicate lines, you can use the following command:

sort -u file.txt

This command will sort the contents of 'file.txt' and remove any duplicate lines, then print the result on the console.

Example 7: Sort a File and Save the Result to Another File

If you want to sort a file and save the result to another file, you can use the following command:

sort file.txt > sorted.txt

This command will sort the contents of 'file.txt' and save the result to 'sorted.txt.'

Example 8: Sort a File and Ignore Leading Whitespaces

If you have a file with leading whitespaces and you want to sort it without considering them, you can use the following command:

sort -b file.txt

This command will sort the contents of 'file.txt' and ignore leading whitespaces, then print the result on the console.

Example 9: Sort a File and Ignore Non-Printable Characters

If you have a file with non-printable characters and you want to sort it without considering them, you can use the following command:

sort -i file.txt

This command will sort the contents of 'file.txt' and ignore non-printable characters, then print the result on the console.

Example 10: Sort a File in Random Order

If you want to sort a file in a random order, you can use the following command:

sort -R file.txt

This command will sort the contents of 'file.txt' in a random order and print the result on the console.

Labels: , , ,

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home