Questions tagged [sort]

Sorting text files or other data, for example with the sort utility.

Filter by
Sorted by
Tagged with
326 votes
5 answers
492k views

Sort and count number of occurrence of lines

I have Apache logfile, access.log, how to count number of line occurrence in that file? for example the result of cut -f 7 -d ' ' | cut -d '?' -f 1 | tr '[:upper:]' '[:lower:]' is a.php b.php a.php c....
Kokizzu's user avatar
  • 9,297
275 votes
18 answers
295k views

How do you sort du output by size?

How do you sort du -sh /dir/* by size? I read one site that said use | sort -n but that's obviously not right. Here's an example that is wrong. [~]# du -sh /var/* | sort -n 0 /var/mail 1.2M /...
xenoterracide's user avatar
214 votes
6 answers
537k views

Sort based on the third column

I'm facing a huge 4-columns file. I'd like to display the sorted file in stdout based on its 3rd column: cat myFile | sort -u -k3 Is that enough to perform the trick?
user1058398's user avatar
  • 4,038
207 votes
11 answers
173k views

List files sorted numerically

I have a bunch of files from log1 to log164. I'm trying to LIST the directory (sorted) in a UNIX terminal but the sort functions are only providing the format like this: home:logs Home$ ls -1 | sort ...
user avatar
169 votes
5 answers
260k views

What is the difference between "sort -u" and "sort | uniq"?

Everywhere I see someone needing to get a sorted, unique list, they always pipe to sort | uniq. I've never seen any examples where someone uses sort -u instead. Why not? What's the difference, and why ...
Benubird's user avatar
  • 5,772
158 votes
3 answers
221k views

Trying to sort on two fields, second then first

I am trying to sort on multiple columns. The results are not as expected. Here's my data (people.txt): Simon Strange 62 Pete Brown 37 Mark Brown 46 Stefan Heinz 52 Tony Bedford 50 John Strange 51 ...
Harry's user avatar
  • 1,843
134 votes
6 answers
187k views

Sorting the output of "find -print0" by piping to the "sort" command

I need to be able to alphabetically sort the output of find before piping it to a command. Entering | sort | between didn't work, so what could I do? find folder1 folder2 -name "*.txt" -print0 | ...
Industrial's user avatar
  • 1,771
117 votes
4 answers
52k views

Does sort support sorting a file in-place, like `sed --in-place`?

Am I blind or is there no option like --in-place for sort? In order to save results to the input file, sed uses -i (--in-place). Redirecting the output of sort to the input file sort < f > f ...
Grzegorz Wierzowiecki's user avatar
104 votes
13 answers
48k views

How can I remove duplicates in my .bash_history, preserving order?

I really enjoying using control+r to recursively search my command history. I've found a few good options I like to use with it: # ignore duplicate commands, ignore commands starting with a space ...
cwd's user avatar
  • 44.6k
100 votes
16 answers
80k views

sort but keep header line at the top

I am getting output from a program that first produces one line that is a bunch of column headers, and then a bunch of lines of data. I want to cut various columns of this output and view it sorted ...
jonderry's user avatar
  • 2,039
49 votes
3 answers
77k views

How to sort big files?

I have a PC with Intel(R) Pentium(R) CPU G640 @ 2.80 GHz and 8 GB of RAM. I am running Scientific Linux 6.5 on it with EXT3 filesystem. On this setup, what is the fastest way I can do a sort -u on a ...
evachristine's user avatar
  • 2,603
46 votes
3 answers
103k views

How can I sort a mix of numeric and alphabetic characters?

How can I sort input such as this using the sort command? I would like the numbers to be sorted numerically before the letters. 10 11 12 1 13 14 15 16 17 18 19 20 21 2 22 3 4 5 6 7 8 9 X Y
user1703276's user avatar
42 votes
6 answers
60k views

Sort all directories based on their size

I'd like to sort all the directories/files in a specific directory based on their size (using du -sh "name"). I need to apply this command to all directories in my location, then sort them based on ...
user1058398's user avatar
  • 4,038
39 votes
6 answers
81k views

Get text-file word occurrence count of all words & print output sorted

I had a command which would work through a text file, count all the occurrences of the words and print it out like this: user@box $˜ magic-command-i-forgot | with grep | and awk | sort ./textfile.txt ...
user avatar
34 votes
2 answers
42k views

How to numerical sort by last column?

I have this input: sdkxyosl 1 safkls 2 asdf--asdfasxy_asd 5 dkd8k jasd 29 sdi44sw 43 asasd afsdfs 10 rklyasd 4 I need this output: sdi44sw 43 dkd8k jasd 29 asasd afsdfs 10 asdf--asdfasxy_asd 5 ...
LanceBaynes's user avatar
  • 39.4k
34 votes
1 answer
5k views

Why is sort changing the order of lines with identical sort keys?

Here is the data: D 2 B 2 A 2 When I run this command: sort -k2,2 file it outputs: A 2 B 2 D 2 My question is that when I specify only the second column -k2,2, why is it that it sorts by the ...
dwwdw's user avatar
  • 925
32 votes
2 answers
3k views

Why is sort -o useful?

UNIX philosophy says: do one thing and do it well. Make programs that handle text, because that is a universal interface. The sort command, at least GNU sort, has an -o option to output to a file ...
strugee's user avatar
  • 14.7k
32 votes
4 answers
12k views

Why does ls sorting ignore non-alphanumeric characters?

When sorting file names, ls ignores characters like -,_. I expected it to use those characters in sorting as well. An example: touch a1 a2 a-1 a-2 a_1 a_2 a.1 a.2 a,1 a,2 Now display these files ...
daniel kullmann's user avatar
31 votes
5 answers
72k views

How is it possible to sort ps command's CPU field?

I'm trying to make a small command that will find the processes that use the most CPU power. Firstly, I use ps aux > file.txt and then cut -c 16-20 file.txt | sort -n | tail -5. The result I get ...
Spyros Chiotakis's user avatar
31 votes
5 answers
35k views

How to list files sorted by modification date recursively (no stat command available!)

How can I get the list of all files under current directory along with their modification date and sorted by that date? Now I know how to achieve that with find, stat and sort, but for some weird ...
alex's user avatar
  • 7,113
30 votes
4 answers
21k views

How do I make ls sort underscore characters first?

I like being able to name files and directories with an underscore prefix if it's something I want to keep separate from other files and directories at the same level. On Windows and Mac, for example, ...
Tom Auger's user avatar
  • 594
29 votes
3 answers
6k views

Scalability of 'sort -u' for gigantic files

What is reasonable scalability limit of sort -u? (in dimensions of "line length", "amount of lines", "total file size") What is Unix alternative for files exceeding this ...
Grzegorz Wierzowiecki's user avatar
29 votes
1 answer
51k views

Sort only on the second column [duplicate]

I'm trying to sort a file where typically (not always) the lines are whatever:[A-Ba-b0-9_]: values where fields are separated with :. Some lines do not follow this pattern, I'm not interested in ...
Jav's user avatar
  • 960
29 votes
3 answers
32k views

What's the default order of Linux sort?

For a long period I thought the default behavior of the sort program was using ASCII order. However, when I input the following lines into sort without any arguments: # @ I got: @ # But according ...
user avatar
29 votes
5 answers
46k views

Specify the sort order with LC_COLLATE so lowercase is before uppercase

Given the file: $ cat file 1 a C B 2 c 3 A b By default sort will: $ sort file 1 2 3 a A b B c C With LC_COLLATE=C so will sort in uppercase letter before lowercase: $ LC_COLLATE=C sort file 1 2 ...
iiSeymour's user avatar
  • 730
28 votes
4 answers
60k views

sort command in unix with numeric sort

I have file1: "$lvl=57" "$lvl=20" "$lvl=48" "$lvl=17" "$lvl=58" File2 I want: "$lvl=17" "$lvl=20" "$lvl=48" "$lvl=57" "$lvl=58" Basically numerically sort of file1.
yisha's user avatar
  • 909
27 votes
3 answers
4k views

Why does sort say that ɛ = e?

ɛ ("Latin epsilon") is a letter used in certain African languages, usually to represent the vowel sound in English "bed". In Unicode it's encoded as U+025B, very distinct from everyday e. However, if ...
Draconis's user avatar
  • 666
27 votes
3 answers
28k views

How to sort the string which combined with string + numeric using bash script?

This is the data what I want to sort. But sort treats the numeric to string, the data it no sorted as I expected. /home/files/profile1 /home/files/profile10 /home/files/profile11 /home/files/...
user760548's user avatar
27 votes
7 answers
127k views

Sorting ls output by date in filename, in reverse order

I can generate output sorted by date in "normal order" with the following command: ls -A -lR | grep "^-" | sed "s/.\{43\}/&\n/" | grep -v "^-" | while read ...
LanceBaynes's user avatar
  • 39.4k
27 votes
1 answer
117k views

Sort data in descending order of first column, for equal values, use second column in ascending order

Allow me to clarify: Assume I have some keywords with frequency of their usage: 12 Hi 7 Hash 7 C++ 9 Superuser 17 Stackoverflow 9 LaTeX 42 Life 9 Ubuntu What I want, is to sort this data ...
Pouya's user avatar
  • 875
26 votes
6 answers
8k views

How can I sort a list with major.minor.patch level and sometimes rc correctly?

I have to sort the following list with a shell script and make the latest version appear on the bottom or top. How would I do that with shell tools only? release-5.0.0.rc1 release-5.0.0.rc2 release-5....
Mandragor's user avatar
  • 1,050
25 votes
1 answer
4k views

How to use "parallel" to speed up "sort" for big files fitting in RAM?

I have a 100 M line file that fits in RAM on a GNU/Linux system. This is rather slow: sort bigfile > bigfile.sorted and does not use all 48 cores on my machine. How do I sort that file fast?
Ole Tange's user avatar
  • 33.7k
23 votes
3 answers
5k views

Why is coreutils sort slower than Python?

I wrote the following script to test the speed of Python's sort functionality: from sys import stdin, stdout lines = list(stdin) lines.sort() stdout.writelines(lines) I then compared this to the ...
augurar's user avatar
  • 621
21 votes
2 answers
75k views

Sort with field separator [duplicate]

I'm trying to sort the /etc/passwd numerically by user id numbersb(third field) in ascending order and then send it to s4. What command would I uses to do that? I'm on this for a while now.
Holy Gaming Empire's user avatar
21 votes
2 answers
77k views

How to sort results from ls command by modification date (directories first)?

I can fire ls -lrt to get files and folders sorted by modification date, but this does not separate directories from files. I want ls to show me first directories by modification date and then ...
jrara's user avatar
  • 2,069
21 votes
5 answers
121k views

Sort the output of find -exec ls

Is it possible to the output of find … -exec ls -ls ; sorted alpabetically, by filename? This is my cron command: find /home/setefgge/public_html -type f -ctime -1 -exec ls -ls {} \; This command ...
MaJ's user avatar
  • 231
20 votes
7 answers
57k views

join : "File 2 not in sorted order"

I've got two files _jeter3.txt and _jeter1.txt I've checked they are both sorted on the 20th column using sort -c sort -t ' ' -c -k20,20 _jeter3.txt sort -t ' ' -c -k20,20 _jeter1.txt #no ...
Pierre's user avatar
  • 1,723
19 votes
7 answers
37k views

Sort the files in the directory recursively based on last modified date

Sort the files in the directory recursively based on last modified date I have modified a lot of files in my directory want to know what are those files by sorting them by the last modified date ...
Danny's user avatar
  • 481
19 votes
1 answer
69k views

How to sort by multiple columns? [duplicate]

I have a tab-separated file, and I want to sort it by its columns 9, 14, and 16. By this I mean that all rows that agree at column 9 should be sorted by column 14, and among these, those that also ...
kjo's user avatar
  • 14.8k
19 votes
3 answers
26k views

How to find the largest open files?

Looking for the largests among the open files by all processes. lsof already has the open files with their sizes. It may be passing the right parameters to lsof and processing the output.
Hakan Baba's user avatar
18 votes
3 answers
3k views

Sort not sorting lines with a pipe '|' in it correctly

I am trying to sort some simple pipe-delimited data. However, sort isn't actually sorting. It moves my header row to the bottom, but my two rows starting with 241 are being split by a row starting ...
user10777668's user avatar
18 votes
1 answer
685 views

Directory listing not fully sorted

I have the following directory listing on a Debian Linux system. However, one strange thing is that the file populate.sql does not seem to be sorted with the rest. -rw-r--r-- 1 user1 user1 10004 Jul ...
kojow7's user avatar
  • 299
17 votes
5 answers
12k views

How to sort human readable size

I'm basically looking for files then sorting by the size. The script works if I don't sort the size by human readable. But I want the size to be human readable. How can I sort sizes that are human ...
tvo000's user avatar
  • 173
17 votes
2 answers
2k views

GNU sort stable sort when sort does not know sort order

I have a two-column file; the file is sorted the way I want it on column 1 already. I would like to sort on column 2, within each column 1 category. However, sort does not understand the sort order of ...
Evan Benn's user avatar
  • 438
17 votes
2 answers
14k views

Grep starting from a fixed text, until the first blank line

I have a file prova.txt like this: Start to grab from here: 1 fix1 fix2 fix3 fix4 random1 random2 random3 random4 extra1 extra2 bla Start to grab from here: 2 fix1 fix2 fix3 fix4 random1546 ...
heisen's user avatar
  • 307
17 votes
4 answers
11k views

sort by hex value

Using coreutils sort, how can I sort numerically by a hexadecimal value (field)? I was expecting something along the lines of sort -k3,3x file_to_sort however, such an x does not exist. Edit: Best ...
stefan's user avatar
  • 1,081
16 votes
2 answers
3k views

How to sort this output 1,10,11..2

Sometimes I need to add more disk to a database; for that, I need to list the disks to see what disks already exist. The problem is that the output is always sorted as 1,10,11,12...2,20,21...3 etc. ...
Potter's user avatar
  • 425
16 votes
2 answers
20k views

sort output by column

I'd like to take this command find -maxdepth 1 -type d | while read -r dir; do printf "%s:\t" "$dir"; find "$dir" | wc -l; done ( from here ). which has an output of basically ./kennel: 11062 ./...
xenoterracide's user avatar
16 votes
6 answers
10k views

Can I parallelize sort?

For example for bzip there is pbzip, a parallel version of bzip. Is there any such parallelization tools for sort to improve performance?
miku's user avatar
  • 673
15 votes
8 answers
4k views

How can I find the most frequent word in a .csv file, ignoring duplicates on each line?

I need to find the 10 most frequent words in a .csv file. The file is structured so that each line contains comma-separated words. If the same word is repeated more than once in the same line, it ...
ginopino's user avatar
  • 370

1
2 3 4 5
20