Linux Shell Sort File According to the Second Column

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

Sort a file by the second column

You can use this to sort the file by second column of your file alphabetically:

file="/home/here.txt"
sort -t":" -k2 $file; # -t is delimeter and -k is column number to sort

Read about sort in man pages to get more info.

Sort according to second column numerically and first alphabetically

First sort by column 1, then by 2:

sort -k1,1 -k2,2n file.txt

linux shell, sort column 1 in ascending order column 3 in descending order

The command sort -k1,1n -k3,3nr should work.
It sorts only regarding column one (That's the difference between -k1 and -k1,1) so it can reach the second argument.

Note that sort -k1,1n -k3nr probably works too.

For more about multiple key sorting : Sorting multiple keys with Unix sort or https://unix.stackexchange.com/questions/52762/trying-to-sort-on-two-fields-second-then-first

Sorting a file by multiple columns using bash sort

You're missing the -n/--numeric-sort option, to sort according to string numerical value, not lexicographically (at least for second and third field):

$ sort -k1,1 -k2,2n -k3,3n file.txt
word01.1 5 8
word01.1 10 20
word01.1 10 30
word01.1 40 50
word01.2 10 25
word01.2 30 50
word01.2 40 50

Note that you can provide a global -n flag, to sort all fields as numerical values, or per key. Format for key is -k KEYDEF, where KEYDEF is F[.C][OPTS][,F[.C][OPTS]] and OPTS is one or more of ordering options, like n (numerical), r (reverse), g (general numeric), h (human numeric), etc.

BASH: File sorting according to file name

Iterate over files. Extract the destination directory name from the filename. Move the file.

for i in *.dlg; do
# extract last number with your favorite tool
n=$( <<<"$i" sed 's/.*[^0-9]\([0-9]*\)\.dlg$/\1/' )
# move the file to proper dir
echo mkdir -p "folder$n"
echo mv "$i" "folder$n"
done

Notes:

  • Do not use upper case variables in your scripts. Use lower case variables.
  • Remember to quote variables expansions.
  • Check your scripts with http://shellcheck.net
  • Tested on repl

update: for OP's foldernaming convention:

for i in *.dlg; do
foldername="$HOME/output/${i%%_*}_${i#*_*_}"
echo mkdir -p "$foldername"
echo mv "$i" "$foldername"
done

sort multiple files according to 2nd column decreasingly

As noted in the comments:

for file in *.txt; do sort -k2nr -o "$file" "$file"; done

The -k2nr sorts on field 2 in reverse numeric order. The -o option followed by one of the input file names (here, the only input file name) safely overwrites the file with the sorted output.

sort a file based on a column in another file

An awk solution: store the 2nd file in memory, then loop over the first file, emitting matching lines from the 2nd file:

awk 'FNR==NR {x2[$1] = $0; next} $1 in x2 {print x2[$1]}' second first

Implementing @Barmar's comment

join -1 2 -o "1.1 1.2 2.2 2.3" <(cat -n first | sort -k2) <(sort second) | 
sort -n |
cut -d ' ' -f 2-

note to other answerers, I tested with these files:

$ cat first
foo x y
bar x y
baz x y
$ cat second
bar x1 y1
baz x2 y2
foo x3 y3

Explanation of

awk 'FNR==NR {x2[$1] = $0; next} $1 in x2 {print x2[$1]}' second first

This part reads the 1st file in the command line paramters (here, "second"):

FNR==NR {x2[$1] = $0; next}

The condition FNR == NR will be true only for the first named file. FNR is awk's "File Record Number" variable, NR is the current record number from all input sources. The current line is stored in an associative array named x2 (not a great variable name) indexed by the first field of the record.

The next condition, $1 in x2, will only start after the file "second" has been completely read. It will look at the first field of the line in file named "first", and the action prints the corresponding line from file "second", which has been stored in the array.

Note that the order of the files in the awk command is important. Since you control the output based on the file named "first", it must be the last file processed by awk.

How can I sort by first column textually and then by the second numerically with 'sort'?

You have to terminate the primary key, otherwise, sort uses all the fields starting from the given one:

sort -k1,1 -k2n


Related Topics



Leave a reply



Submit