How to sort lines in text files in Linux | sort Command

Configurare noua (How To)

Situatie

SORT command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ASCII. Using options in the sort command can also be used to sort numerically.

Solutie

Pasi de urmat

Let’s start with a simple example. Consider a file named ‘file.txt’ with the following content:

cat file.txt

Here we used cat command to display the content inside the file name ‘file.txt’.

To sort the lines alphabetically, you can use the following command:

sort file.txt

sort lines in text using sort command

This command does not actually change the input file, i.e. file.txt . We can verify this using cat command .

no change is done to real file

How to Sort Lines in Text Files with Uppercase and Lowercase Using sort Command

Sort function with mix file i.e. uppercase and lower case: When we have a mix file with both uppercase and lowercase letters then first the upper case letters would be sorted following with the lower case letters.

Example: If we have a text file which has both Uppercase andlowercase characters.

cat mix.txt

Here we used cat command to display the content inside the file name ‘mix.txt’.

To sort the lines alphabetically which contain uppercase and lowercase letters, you can use the following command:

sort mix.txt

Lightbox

Tip solutie

Permanent

Voteaza

(6 din 12 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?