How to sort a file, based on its numerical values for a field?
Take a peek at the man page for sort...
-n, --numeric-sort
compare according to string numerical value
So here is an example...
sort -n filename
How to sort text file based on number after first letter
You should sort as numerically,
sort --numeric-sort --field-separator "-" --key 2 some.txt
or a shorter version
sort -n -t "-" -k 2 some.txt
Sort a text file by numeric value of the last word on each line
If you reverse the file, you can then sort from the numbers and reverse back
$ sed 's/[^0-9]*\(.*\)/\1 &/' input_file | sort -rn | sed 's/[^a-z]*\(.*\)/\1/'
blabla 45
blabla 23
blabla 12
blabla 5
blabla 4
Sort numerically by all fields in bash
This is actually quite a subtle problem, to do with the way sort
handles numeric fields. The upshot is that you need to explicitly tell sort
to sort numerically on each key field:
sort -t, -k1,1n -k2,2n -k3,3n -k4,4n
If you don't do that, the info
section for GNU sort
says, slightly paraphrased,
sort
would have used all characters beginning in the [first] field and
extending to the end of the line as the primary numeric key. For
the large majority of applications, treating keys spanning more
than one field as numeric will not do what you expect.
which neatly summarises what you saw!
Obviously specifying keys explicitly is going to make sort
inconvenient to use on files with arbitrarily long lists of numbers on each line. As a hack, you could try GNU sort
with its version sort option, -V
.
sort -V
which appears to do the right thing on your particular data. I've tested sort -V
on lines with 600 numbers, and it works fine.
sort numerically based on the first column
sort
reads from stdin
and command-line as well. Thus if you have a file you can:
sort < file
# or
sort file
if you want sort based on first column you can:
sort -k1 < file
But if fact it does affect the output since by default it does not care about numerical order. Thus you should add -n
option:
-n, --numeric-sort compare according to string numerical value
and doing it like:
sort -k1 -n < file
it outputs:
061 data1
238 data6
264 data7
439 data9
682 help
1264 moredata
2305 data2
4080 data3
5640 otherdata
9251 data4
11844 data5
33940 data8
and if you provide it with -r
it print in reverse order:
33940 data8
11844 data5
9251 data4
5640 otherdata
4080 data3
2305 data2
1264 moredata
682 help
439 data9
264 data7
238 data6
061 data1
How to do a bash sort by numerical value contained between two different delimiters in the set of strings
We can use bash sort
;
ls | sort -g -t '_' -k 2
-g
: Sort on general-numeric-t
: field-separator (_
)-k
: Key,2
means: Get second col (after-t
)
Example:
-> ls -t
DataFile_42.txt DataFile_162.txt DataFile_15.txt DataFile_1.txt
->
-> ls | sort -g -t '_' -k 2
DataFile_1.txt
DataFile_15.txt
DataFile_42.txt
DataFile_162.txt
If you add the --debug
option, we can visually see how -t
and -k
works:
-> ls | sort -g -t '_' -k 2 --debug
sort: text ordering performed using simple byte comparison
sort: key 1 is numeric and spans multiple fields
DataFile_1.txt
__
______________
DataFile_15.txt
___
_______________
DataFile_42.txt
___
_______________
DataFile_162.txt
____
________________
->
How to sort a file based on key name instead of its position in unix?
sort
doesn't have a concept of named keys, but you can perform a Schwartzian transform to temporarily add the key as a prefix to the line, sort on the first field, then discard it.
sed 's/\(.*\)\(party_id="[^"]*"\)/\2 \1\2/' file |
sort -t ' ' -k1,1 |
cut -f2-
(where the whitespace between the two first back references and in the sort -t
argument is a literal tab, which however Stack Overflow renders as a sequence of spaces).
Sorting data based on second column of a file
You can use the key
option of the sort
command, which takes a "field number", so if you wanted the second column:
sort -k2 -n yourfile
-n
,--numeric-sort
compare according to string numerical value
For example:
$ cat ages.txt
Bob 12
Jane 48
Mark 3
Tashi 54
$ sort -k2 -n ages.txt
Mark 3
Bob 12
Jane 48
Tashi 54
sort alphanumerically with priority for numbers in linux
So, basically, you're asking to sort the first field numerically in descending order, but if the numeric keys are the same, you want the second field to be ordered in natural, or ascending, order.
I tried a few things, but here's the way I managed to make it work:
sort -nk2 file.txt | sort -snrk1
Explanation:
The first command sorts the whole file using the second, alphanumeric field in natural order, while the second command sorts the output using the first numeric field, shows it in reverse order, and requests that it be a "stable" sort.
-n
is for numeric sort, versus alphanumeric, in which 6 would come before 60.-r
is for reversed order, so from highest to lowest. If unspecified, it will assume natural, or ascending, order.-k
which key, or field, to use for sorting order.-s
for stable ordering. This option maintains the original record order of records that have an equal key.
Bash:: How to sort according to first numeric column of file?
How about this:
sed -e 's/^[^0-9.]*\([0-9.]\+\).*$/\1\t\0/' input | sort -n | cut -f 2-
We extract the first numeric field and we insert it to the beginning of each line. Then we sort numerically all lines and then we remove the stuff that we added in the first step.
Related Topics
Can Read() Function on a Connected Socket Return Zero Bytes
Using Mono-Service to Wrap a Windows Service on Linux
How to Compile Glut + Opengl Project with Cmake and Kdevelop in Linux
Default Field Separator for Awk
How to Split a File and Keep the First Line in Each of the Pieces
Linux: Find All Symlinks of a Given 'Original' File? (Reverse 'Readlink')
What Are the Real Rules for Linux Usernames on Centos 6 and Rhel 6
How Does Pthread Implemented in Linux Kernel 3.2
Explaining the 'Find -Mtime' Command
How to Fix Conda Update Conda Permission Error
What Is an Anonymous Inode in Linux
Bash Read/Write File Descriptors -- Seek to Start of File
Virtualenv: Workon Command Not Found
Hardware Cache Events and Perf
How to Kill All Linux Processes That Are Older Than a Certain Age