Sort by numbers linux

sort command in unix with numeric sort

When I run a plain Unix sort on your File1, with no options, I get your File2. What are you doing differently? What are you leaving out of this question?

4 Answers 4

I like the -V / —version-sort option found in a few implementations of sort (from GNU sort ): it behaves very well for many situations mixing strings and numbers

I use this option very often.

In the same direction, with some implementations of ls , ls -v for version-sort ls (from GNU ls ).

You need to tell sort -n to sort on the part after the = :

@KasiyA -k defines a sort key. See the man page for details. -k2n defines a sort key starting at the second field and ending at the end of the line and makes it a numeric sort key.

I found that, you just run sort -h , it will work. They call it —human-numberic-sort .

-n, —numeric-sort
compare according to string numerical value

No, all those lines have the same ranking with sort -n since they don’t start with a number. The reason it sorts them is the last-resort full-line sort (lexically, not numerically) done for lines with the same ranking. That would sort «$lvl=17» before «$lvl=2» .

To paraphrase, -n and -g are redundant here as the input is not numerical. So this answer is misleading, hence the downvote (also note that -g and the long options are GNU specific).

That’s still for sorting numerical values, the difference with -n is that it’s not limited to decimal integers. That would still sort «$lvl=17» before «$lvl=2» as part of the last-resort sorting.

You must log in to answer this question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.12.43529

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Linux and Unix sort command tutorial with examples

Tutorial on using sort, a UNIX and Linux command for sorting lines of text files. Examples of alphabetical sorting, reverse order sorting, sorting by number and mixed case sorting.

Читайте также:  Смена имени пк alt linux

The UNIX and Linux sort command

  • March 21, 2011
  • Updated May 24, 2023

What is the sort command? ¶

The sort command is a command line utility for sorting lines of text files. It supports sorting alphabetically, in reverse order, by number, by month and can also remove duplicates. The sort command can also sort by items not at the beginning of the line, ignore case sensitivity and return whether a file is sorted or not.

sort is part of the The GNU Core Utilities, which is open sourced under the GNU General Public License.

How to sort alphabetically ¶

The sort tool will sort lines alphabetically by default. Running sort filename writes the contents of the filename in alphabetical order to standard output.

Suppose a file exists with the following list of metal bands that needs to be sorted in alphabetical order. The file is saved as bands.txt .

Motörhead ACDC Sepultura Carcass Opeth 

The sort command allows us to sort the file alphabetically.

sort bands.txt ACDC Carcass Motörhead Opeth Sepultura 

A terminal showing a file being sorted alphabetically

The GNU Coreutils version of sort is written in C and you can read the source code.

How to sort in reverse order ¶

To sort in reverse order pass the -r option to sort . This will sort in reverse order and write the result to standard output.

Using the same list of metal bands from the previous example this file can be sorted in reverse order with the -r option.

sort -r bands.txt Sepultura Opeth Motörhead Carcass ACDC 

How to sort by number ¶

To sort by number pass the -n option to sort . This will sort from lowest number to highest number and write the result to standard output.

Suppose a file exists with a list of items of clothing that has a number at the start of the line and needs to be sorted numerically. The file is saved as clothes.txt .

3. Brown shoes 5. Blue tie 1. White shirt 11. Jeans 4. Underpants 

By passing sort the -n option the file is ordered numerically.

sort -n clothes.txt 1. White shirt 3. Brown shoes 4. Underpants 5. Blue tie 11. Jeans 

A terminal showing a file being sorted numerically

How to sort mixed-case text ¶

To sort mixed-case text pass the -f option to sort . This will ignore case sensitivity when sorting and write the result to standard output.

If a file has uppercase and lowercase content sort will order uppercase first. Suppose a file exists with a list of names in a file called names.txt .

By default the sort tool will sort uppercase characters first.

sort names.txt Sam Sarah sally steven 

To sort and ignore case use the -f option.

sort -f names.txt sally Sam Sarah steven 

How to check if a file is already sorted ¶

To check if a file is already sorted pass the -c option to sort . This will write to standard output if there are lines that are out of order.

Suppose a file exists with a list of cars called cars.txt .

The sort tool can be used to understand if this file is sorted and which lines are out of order.

sort -c cars.txt sort: cars.txt:3: disorder: BMW 

If there is no output then the file is considered to be already sorted.

Читайте также:  Linux as ethernet switch

How to sort and remove duplicates ¶

To sort and remove duplicates pass the -u option to sort . This will write a sorted list to standard output and remove duplicates.

Suppose a file exists with a list of breakfast cereals to sort. This file contains a number of duplicates. This is saved in the file breakfast.txt .

Cornflakes Sultana Bran Weetabix Sultana Bran Cornflakes Shredded Wheat Cherrios Weetabix 

By using the -u option this file can be sorted and stripped of duplicates.

sort -u breakfast.txt Cherrios Cornflakes Shredded Wheat Sultana Bran Weetabix 

A terminal showing a file being sorted with duplicates removed

sort has great documentation. You can read it by typing man sort in the terminal.

How to sort by month ¶

To sort by month pass the -M option to sort . This will write a sorted list to standard output ordered by month name.

Suppose the following file exists and is saved as months.txt .

February January March August September 

Using The -M option with sort allows us to order this file.

sort -M months.txt January February March August September 

How to sort by items not at the beginning of the line ¶

To sort by items not at the beginning of the line pass the -k option to sort along with a number of value of the field to sort on. This will write the result to standard output.

Suppose a file exists with a list of orders that is saved as orders.txt .

1023 AcmeCo "Bouncey Castle" 1003 FooCo "Fluffy Toy" 1013 AcmeCo "Edible Hat" 1042 FooCo "Whoopie Cushion" 

The file needs to be sorted by the name of the company that placed them. By using the -k option and passing it a number of the key this can be achieved.

sort -k 2 orders.txt 1023 AcmeCo "Bouncey Castle" 1013 AcmeCo "Edible Hat" 1003 FooCo "Fluffy Toy" 1042 FooCo "Whoopie Cushion" 

How to sort a CSV file ¶

To sort by a delimiter pass the -t option to sort along with the delimiter value. For a CSV file this would be , . This can be combined with the -k option to sort on fields within a CSV. The result will be written to standard output.

Suppose a file exists with a list of cheeses that is saved as cheese.csv .

2,Maroilles,1.13 3,Stinking Bishop,1.65 1,Brie de Meaux,1.99 4,Munster,1.29 

The file may be sorted by the name of the cheese using a combination of the -k and -t options.

sort -k 2 -t , cheese.csv 1,Brie de Meaux,1.99 2,Maroilles,1.13 4,Munster,1.29 3,Stinking Bishop,1.65 

To sort on the most expensive cheese the numeric and reverse options can be used.

sort -k 3 -t , -n -r cheese.csv 1,Brie de Meaux,1.99 3,Stinking Bishop,1.65 4,Munster,1.29 2,Maroilles,1.13 

A terminal showing a csv file being sorted on different fields

The original version of sort was written by Ken Thompson. The GNU Coreutils version was written by Mike Haertel and Paul Eggert.

Further reading ¶

Tags

Can you help make this article better? You can edit it here and send me a pull request.

See Also

  • Linux and Unix wc command tutorial with examples
    Mar 19, 2011
    Tutorial on using wc, a UNIX and Linux command for printing newline, word and byte counts for files. Examples of printing the number of lines in a file, printing the number of characters in a file and printing the number of words in a file.
  • Linux and Unix head command tutorial with examples
    Nov 3, 2010
    Tutorial on using head, a UNIX and Linux command for outputting the first part of files. Examples of outputting the first ten lines of a file, limiting the number of lines, limiting the number of bytes, showing multiple files and using pipes.
  • Linux and Unix tr command tutorial with examples
    Jul 23, 2010
    Tutorial on using tr, a UNIX and Linux command for translating or deleting characters. Examples of converting uppercase to lowercase, deleting specific characters, squeezing repeating patterns and basic finding and replacing.
Читайте также:  Linux ubuntu ftp сервер

Источник

In bash, how to sort strings with numbers in them?

how can I list them in Bash so that they are in ascending numeric order based on the number part of the string. So the resulting order is cwcch1.pdf, cwcch2.pdf, . cwcch9.pdf, cwcch10.pdf , etc. What I’m ultimately trying to do is concatenate the pdfs with pdftk with something like the following

pdftk `ls *.pdf | sort -n` cat output output.pdf 

Thanks for all the great answers to this. As always with Unix, there are many different excellent ways to skin this cat.

7 Answers 7

Something like this might do what you want, though it takes a slightly different approach:

pdftk $(for n in ; do echo cwcch$n.pdf; done) cat output output.pdf 

Your sort may have the ability to do this for you:

Excerpt of relevant entry in sort man page: -V, —version-sort natural sort of (version) numbers within text

This is what you need. But if your sort does not supply this option take a look at this post: stackoverflow.com/a/4495368/1240018

For this particular example you could also do this:

That is, sort numerically (-n) on the second field (-k2) using ‘h’ as the field separator (-th).

Splitting and then sorting on one field — that’s a great tip that I’m sure will be handy in future, thanks.

You can use the -v option in GNU ls : natural sort of (version) numbers within text.

This does not work with BSD ls (e.g. on OS X), where the -v option has a different meaning.

Use shell expansion directly in a commandline. The expansion should order them properly. If I understand pdftk ‘s commandline syntax properly, this will do what you want:

# shell expansion with square brackets pdftk cwcch5.pdf cwcch15.pdf cat output output.pdf # shell expansion with curly braces pdftk cwcch,>.pdf cat output output.pdf 

Or you can try a different approach. When I need to do something like this, I usually try to get my numbers formatted properly ahead of time. If I’m coming into it late and the PDFs are already numbered like your example, I’ll use this to renumber:

# rename is rename.pl aka prename -- perl rename script # this adds a leading zero to single-digit numbers rename 's/(\d)/0$1/' cwcch5.pdf 

Now the standard ls sorting will work properly.

Источник

Оцените статью
Adblock
detector