简体   繁体   中英

Linux commands: how to sort a file based on differences between columns?

I have a text file with lines of the following format:

26 20 18 19 43 93 
3 16 6 7 47 1 
19 9 14 37 22 0 
25 27 14 10 62 29 
19 6 10 27 33 5 
24 14 15 20 21 6 
10 17 15 8 47 31 

Is there a way of using 'sort' under Linux to sort the file based on the differences of any two columns, eg, differences between column 3 and 4?

I know sort can do it based on a specific column, eg,

sort -r -k 3n data.csv

but not sure if it can sort based on the differences.

Other Linux commands are also welcome. Any scripting languages such as Perl or Python can easily fulfill this task but I'm curious of Linux commands.

EDIT: by difference I meant numeric differences, and it has a sign. For example, to sort based on Column3-Column4, it means to sort by -1, -1, -23, 4, -17, ...

I guess for "difference" you meant the absolute value abs() . so you can do this:

 awk '{d=$3-$4;$0=(d>0?d:-d) "#"$0}7' file|sort -n|sed 's/^.*#//'

it outputs:

26 20 18 19 43 93
3 16 6 7 47 1
25 27 14 10 62 29
24 14 15 20 21 6
10 17 15 8 47 31
19 6 10 27 33 5
19 9 14 37 22 0

The standard sort command doesn't do expressions of fields, as far as I know. But you can do a sort of shell version of a Schwartzian transform:

awk '{print $3-$4,$0}' | 
sort -n -k1,1 | 
sed 's/^[^ ]* //'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM