New answers tagged

1

If your find has printf, print out the mtime in seconds followed by the filename, then use sort, and finally cut: find . -type f -printf "%T@\t%f\n" | sort -k 1n -k 2 | cut -f 2- The find outputs TIMESTAMP FILENAME on each line. The sort first sorts the timestamps in numerical order. If the timestamps are equal, it will use the filename as a last resort. ...


3

To sort from highest to lowest: grep -rc 'Author' $1 | sort -r -t ':' -k2,2n -r sorts in reverse order, that is, from highest to lowest. -t introduces the separator. -k introduces which fields to sort by. The fields are separated by the separator defined by -t. This syntax means to sort by all of the fields between 2 and 2 (so just the second field). The ...


3

grep -rc 'Author' $1 | sort -t : -k 2,2n is good if you want the number of lines containing the keyword, regardless how many times it repeats on any given line. If you want the actual word count, you should use this echo $1:$(grep -o 'Author' $1|wc -l) | sort -t : -k 2,2n -o option for grep is most probably available on the gnu version of grep. If you ...


0

I created a perl script to do exactly this, you can input a regex to sort a file by the first capture. then you can set a flag to do either string or numerical comparison. just toss this code sample into a .pl file. it's pretty simple and the logic really just sits on lines 20-37. #! /usr/bin/perl # Created by pete Nixon use Getopt::Long; use strict; use ...


3

This is a locale issue, compare the output of locale in your two environments, and adjust the one where you want the output changed. For example, on Linux (the sort version or OS shouldn't matter much): $ LC_ALL=C sort t -------- ----------- -=-=-=-=-=- =========== $ LC_ALL=en_US.UTF-8 sort t =========== -=-=-=-=-=- -------- -----------


0

sed $'s/\t/ /g' my_file | tr -s " " | sort -t" " -k 3 the first sed command, replaces all tab characters with single space. tr -s " " means squeeze multiple consecutive white space characters to only one space character. If a numeric sort is needed, you can use sed $'s/\t/ /g' my_file | tr -s " " | sort -t" " -n -k 3 of course this did not address ...


1

Try this: sort -n --k3 <file> For example: $ sort -n -k3 test HELIX cp9(plasmid 9586 bp DNA helix 29-AUG-2011 HELIX lp25(plasmid 24437 bp RNA linear 29-AUG-2011 HELIX lp28-1(plasmid 25455 bp DNA linear 29-AUG-2011 HELIX chromosome 911724 bp ...


2

As Tom Zych noted paste -d ' ' <(echo -e "1 5\n1 4\n8 2\n1 9" | cut -f1 -d' ' | sort -n ) <(echo -e "1 5\n1 4\n8 2\n1 9" | cut -f2 -d' ' | sort -n )


1

If I were doing that, I’d first split up the lines into fields, each field going into its own file; could use cut or awk or various other tools for that. Then sort each file separately. Then paste them back together.


19

sort -k 1.2bn < file Sorts numerically on a key starting with the 2nd character of the 1st field ignoring leading blanks (and ending at the end of the line, but that doesn't matter for a numerical sort which only considers the initial sequence of decimal digits). Note that if there's a tie, like in between these two lines: F91HE*-K92HA 7.242 ...


1

Is this a good enough approximation? cat garble RNU2-46P 0 RNU2-2P 9 RNU2-63P 5 RNU2-36P 6 RNU2-56P 8 RNU2-17P 0 sort -t'-' -k2n garble RNU2-2P 9 RNU2-17P 0 RNU2-36P 6 RNU2-46P 0 RNU2-56P 8 RNU2-63P 5


1

If you know that each line consists of a single word repeated a number of times, you can do: awk -v n=$number_of_rows ' NR == n+1 {exit} {print $1, NF} ' file | sort -k2,2rn -k1,1


0

Your use of tr is clever. But you need to sort before you use uniq, because uniq only looks at adjacent lines. So we have cat file.txt | sort | uniq -c | sort -r | awk '{print $2, $1}' | head -n 10 Also as you can see the use of -k and -n for sort is unnecessary in this case (though not wrong).


1

The answer to the first question would be (if anyone is interested ?) tr [:space:] '\n' <$1| sort |uniq -c|sort -k1rn -k2n|awk '{print $2,$1}'|head -12 I still don't know how to do this part . As an extra feature , i'd like the script to seach number of occurencies of words in the first m lines of file.


4

From man sort: -k, --key=POS1[,POS2] start a key at POS1 (origin 1), end it at POS2 (default end of line) ... POS is F[.C][OPTS], where F is the field number and C the character position in the field; both are origin 1. If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace. OPTS is one ...


0

Please try: LC_COLLATE=C sort myfile encountered similar behavior in the past and this one suggested by a coworker helped me


0

I've found empirically that -k1,1 puts the "A10"s together, and the "A-10"s together, but I don't understand why. This is with sort from GNU coreutils 8.4.


2

Use sort's -h option instead of -n: du -sh /* | sort -hr | head -n20


0

Thanks to the ideas from comments and other sources I was finally able to write this code and answer my own question: inputStream | awk -F'\t' -v OFS="\t" '{ if ( col1 == ""){ for (i=1;i<=NF;i++){ if ($i == "BlueId"){ col1=i; } else if ($i ...



Top 50 recent answers are included