Tag Info

Hot answers tagged

70

Why not to use built-in ls feature for this particular case, namely -v natural sort of (version) numbers within text For example ls -1v log*


68

If you have GNU coreutils (common in most Linux distributions), you can use du -sh * | sort -h. The -h option tells sort that the input is the human-readable format (number with unit). This feature was added to GNU Core Utilities 7.5 in Aug 2009.


43

It is not just GNU sort that has it. BSD sort has it too. And as to why? (I thought it was a good question too...) From the man page: "The argument given is the name of an output file to be used instead of the standard output. This file can be the same as one of the input files." You can't go to the same file with redirection, the output redirection ...


39

sort | uniq existed before sort -u, and is compatible with a wider range of systems, although almost all modern systems do support -u -- it's POSIX. It's mostly a throwback to the days when sort -u didn't exist (and people don't tend to change their methods if the way that they know continues to work, just look at ifconfig vs. ip adoption). The two were ...


30

sort has the -o, --output option that take a filename as argument. If it is the same as the input files, it write the result to a temporary file, then overwrite the original input file (somewhat as sed do). From GNU sort info page: `-o OUTPUT-FILE' `--output=OUTPUT-FILE' Write output to OUTPUT-FILE instead of standard output. Normally, `sort' ...


27

Some versions of sort have a -z option, which allows for null-terminated records. find folder1 folder2 -name "*.txt" -print0 | sort -z | xargs -r0 myCommand Additionally, you could also write a high-level script to do it: find folder1 folder2 -name "*.txt" -print0 | python -c 'import sys; sys.stdout.write("\0".join(sorted(sys.stdin.read().split("\0"))))' ...


26

One difference is that uniq has a number of useful additional options, such as skipping fields for comparison and counting the number of repetitions of a value. sort's -u flag only implements the functionality of the unadorned uniq command.


25

That's the last resort comparison. When comparing two lines, if all the keys compare equal, then as a last resort, a basic string comparison of the whole lines is performed (-r still applies but not the other options). That behavior is specified by POSIX: Except when the -u option is specified, lines that otherwise compare equal shall be ordered as if ...


23

Stealing Andy's idea and making it a function so it's easier to use: # print the header (the first line of input) # and then run the specified command on the body (the rest of the input) # use it in a pipeline, e.g. ps | body grep somepattern body() { IFS= read -r header printf '%s\n' "$header" "$@" } Now I can do: $ ps -o pid,comm | body ...


22

Try using the -k flag to count 1K blocks intead of using human-readable. Then, you have a common unit and can easily do a numeric sort. du -ck | sort -n You don't explictly require human units, but if you did, then there are a bunch of ways to do it. Many seem to use the 1K block technique above, and then make a second call to du. ...


22

A key specification like -k2 means to take all the fields from 2 to the end of the line into account. So Villamor 44 ends up before Villamor 50. Since these two are not equal, the first comparison in sort -k2 -k1 is enough to discriminate these two lines, and the second sort key -k1 is not invoked. If the two Villamors had had the same age, -k1 would have ...


18

You can keep the header at the top like this with bash: command | (read -r; printf "%s\n" "$REPLY"; sort) Or do it with perl: command | perl -e 'print scalar (<>); print sort { ... } <>'


18

This is very similar to this question. The trouble is that you have an alphanumeric field that you are sorting on, and -n doesn't treat that sensibly, however version sort (-V) does. Thus use: sort -V Note that this is currently, AFAIK, a GNU sort only solution.


18

With POSIX compliant sorts and uniqs (GNU uniq is currently not compliant in that regard), there's a difference in that sort uses the locale's collating algorithm to compare strings (will typically use strcoll() to compare strings) while uniq checks for byte-value identity (will typically use strcmp()). That matters for at least two reasons. In some ...


18

Izkata's comment revealed the answer: locale-specific comparisons. The sort command uses the locale indicated by the environment, whereas Python defaults to a byte order comparison. Comparing UTF-8 strings is harder than comparing byte strings. $ time (LC_ALL=C sort <numbers.txt >s2.txt) real 0m5.485s user 0m14.028s sys 0m0.404s How about ...


17

Sorting the history This command works like sort|uniq, but keeps the lines in place nl|sort -k 2|uniq -f 1|sort -n|cut -f 2 Basically, prepends to each line its number. After sort|uniq-ing, all lines are sorted back according to their original order (using the line number field) and the line number field is removed from the lines. This solution has ...


17

With awk, you could do: awk -F, -vOFS=, '{l=$0; $3=""}; ! ($0 in seen) {print l; seen[$0]}'


17

sort -k3,3 myFile would display the file sorted by the 3rd column assuming the columns are separated by sequences of blanks (ASCII SPC and TAB characters in the POSIX/C locale), according to the sort order defined by the current locale. Note that the leading blanks are included in the column (the default separator is the transition from a non-blank to a ...


17

Using the sort command will probably be the fastest option. But you'll probably want to fix the locale to C. sort -u doesn't report unique lines, but one of each set of lines that sort the same. In the C locale, 2 different lines necessarily don't sort the same, but that's not the case in most UTF-8 based locales on GNU systems. Also, using the C locale ...


16

My shortest method uses zsh: print -rl **/*(.Om) If you have GNU find, make it print the file modification times and sort by that. I assume there are no newlines in file names. find . -type f -printf '%T@ %p\n' | sort -k 1 -n | sed 's/^[^ ]* //' If you have Perl (again, assuming no newlines in file names): find . -type f -print | perl -l -ne ' ...


16

bash's braces, {}, will enumerate them in order: for file in log{1..164}.gz; do process "$file" done


16

You're looking for uniq -c If the output of that is not to your liking, it can be parsed and reformatted readily. For example: $ uniq -c logfile.txt | awk '{print $2": "$1}' 27.33.65.2: 2 58.161.137.7: 1 121.50.198.5: 1 184.173.187.1: 3


16

GNU sort (which is the default on most Linux systems), has a --parallel option. From http://www.gnu.org/software/coreutils/manual/html_node/sort-invocation.html: ā€˜--parallel=nā€™ Set the number of sorts run in parallel to n. By default, n is set to the number of available processors, but limited to 8, as there are diminishing performance gains after ...


15

Pipe the lines through sort -n -r -k2. Edited to sort from largest to smallest.


15

Let's see, !a[$0]++ first a[$0] we look at the value of a[$0] (array a with whole input line ($0) as key). If it does not exist ( ! is negation in test will eval to true) !a[$0] we print the input line $0 (default action). Also, we add one ( ++ ) to a[$0], so next time !a[$0] will evaluate to false. Nice, find!! You should have a look at code ...


15

Here is the processing: a[$0]: look at the value of key $0, in associative array a. If it does not exist, create it. a[$0]++: increment the value of a[$0], return the old value as value of expression. If a[$0] does not exist, return 0 and increment a[$0] to 1 (++ operator returns numeric value). !a[$0]++: negate the value of expression. If a[$0]++ return ...


14

With GNU ls (i.e. on Linux, Cygwin, or other systems that have GNU ls specifically installed): ls -v In zsh: echo *(n) In other shells: echo log?.gz log??.gz log???.gz Replace echo by printf '%s\n' if you want each file name on a separate line. If you want file metadata as well (ls -l) and you don't have GNU ls, you'll need to call ls separately ...


14

Assuming you have enough memory to slurp the file, you could try perl -e 'use List::Util 'shuffle'; @k=shuffle(<>); print @k[0..999]' file.bed Since you want to do this 10000 times, I would recommend integrating the repetition into the script and shuffling the indices instead of the array itself to speed things up: $ time perl -e 'use List::Util ...


14

You could do something like: awk '{print NF,$0}' file | sort -nr | cut -d' ' -f 2- Basically use awk to print the number of fields then sort by that number an then remove the number. You could do all that in awk (or perl etc) but you get the idea.


13

Sorting depends on the locale; specifically, it depends on $LC_COLLATE (possibly overridden by $LC_ALL), falling back to $LANG if it doesn't exist. The command locale will show you what values you're effectively working with. See man 3 strcoll, man 3 setlocale, etc. LC_COLLATE=C (or POSIX or no locale at all) results in a strict byte-by-byte comparison. ...



Only top voted, non community-wiki answers of a minimum length are eligible