Bash/Linux Sort by 3rd column using custom field seperator
This makes it:
$ sort -t'.' -n -k3 a
output/rtp.0.0.raw
output/rtp.0.1.raw
output/rtp.0.2.raw
output/rtp.0.3.raw
output/rtp.0.4.raw
output/rtp.0.5.raw
output/rtp.0.6.raw
output/rtp.0.7.raw
output/rtp.0.8.raw
output/rtp.0.9.raw
output/rtp.0.10.raw
output/rtp.0.11.raw
output/rtp.0.12.raw
output/rtp.0.13.raw
output/rtp.0.14.raw
output/rtp.0.15.raw
output/rtp.0.16.raw
output/rtp.0.17.raw
output/rtp.0.18.raw
output/rtp.0.19.raw
output/rtp.0.20.raw
As you see we need different options:
-t'.'
to set the dot.
as the field separator.-n
to make it numeric sort.-k3
to check the 3rd column.
Update
This also makes it:
$ sort -t'.' -V -k2 a
output/rtp.0.0.raw
output/rtp.0.1.raw
output/rtp.0.2.raw
output/rtp.0.3.raw
output/rtp.0.4.raw
output/rtp.0.5.raw
output/rtp.0.6.raw
output/rtp.0.7.raw
output/rtp.0.8.raw
output/rtp.0.9.raw
output/rtp.0.10.raw
output/rtp.0.11.raw
output/rtp.0.12.raw
output/rtp.0.13.raw
output/rtp.0.14.raw
output/rtp.0.15.raw
output/rtp.0.16.raw
output/rtp.0.17.raw
output/rtp.0.18.raw
output/rtp.0.19.raw
output/rtp.0.20.raw
As you see we need different options:
-t'.'
to set the dot.
as the field separator.-V
to make it sort based on version.-k2
to check the 2nd column.
How to sort data by the numbers in third column?
Here you go:
sort -n -k 3 test.txt
From man sort:
-n, --numeric-sort compare according to string numerical value
-k, --key=KEYDEF sort via a key; KEYDEF gives location and type
KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a
field number and C a character position in the field; both are origin 1, and
the stop position defaults to the line's end. If neither -t nor -b is in
effect, characters in a field are counted from the beginning of the preceding
whitespace. OPTS is one or more single-letter ordering options [bdfgiMhnRrV],
which override global ordering options for that key. If no key is given, use
the entire line as the key.
and also interesting:
-t, --field-separator=SEP use SEP instead of non-blank to blank transition
which tells us that the F
fields are separated by whitespace.
Sort a tab delimited file based on column sort command bash
To sort on the fourth column use just the -k 4,4
selector.
sort -t $'\t' -k 4,4 <filename>
You might also want -V
which sorts numbers more naturally. For example, yielding 1 2 10
rather than 1 10 2
(lexicographic order).
sort -t $'\t' -k 4,4 -V <filename>
If you're getting errors about the $'\t'
then make sure your shell is bash. Perhaps you're missing #!/bin/bash
at the top of your script?
Linux shell sort file according to the second column?
If this is UNIX:
sort -k 2 file.txt
You can use multiple -k
flags to sort on more than one column. For example, to sort by family name then first name as a tie breaker:
sort -k 2,2 -k 1,1 file.txt
Relevant options from "man sort":
-k, --key=POS1[,POS2]
start a key at POS1, end it at POS2 (origin 1)
POS is F[.C][OPTS], where F is the field number and C the character position in the field. OPTS is one or more single-letter ordering options, which override global ordering options for that key. If no key is given, use the entire line as the key.
-t, --field-separator=SEP
use SEP instead of non-blank to blank transition
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
unix sort on column without separator
$ sort --key=1.3,1.3 inputfile
pp0dd
aa1bb
aa3ya
ax5aa
fg7ds
man page of 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 or more single-letter ordering options, which override global ordering options for that key. If no key is given, use the entire line as the key.
With --key=1.3,1.3, you said that there only one field (the entire line) and that you're comparing the third character position of this field.
Sorting a tab delimited file
Using bash, this will do the trick:
$ sort -t$'\t' -k3 -nr file.txt
Notice the dollar sign in front of the single-quoted string. You can read about
it in the ANSI-C Quoting sections of the bash man page.
Related Topics
Bash Shell: Cannot Use Variable $ as a Path to Run Tar
Sed Not Working [Unterminated 'S' Command]
How to Modify Eip's Tracee Forked Procee
How to Set a Non-Standard Baudrate on a Serial Port Device on Linux
Write to Port 0Cf8H Fails with Segfault
How to Draw 2D Diagram in Linux
How to Use a Samba Server Location for Gopath
Bash: Checking If Files Are Duplicates Within a Directory
How to Split Flv File by Size Using Ffmpeg or Mencoder or Smth Else
Reading Complete Line in 'For' Loop with Spaces, Tabs with Multiple Input Files
Will Data Written via Write() Be Flushed to Disk If a Process Is Killed
Average of Multiple Files Without Considering Missing Values
Filter a Content File to Table
Read Data from Proc/Sys/Kernel/