我有一个具有以下结构的文件:
import xlsxwriter
table = [[a, b], [c, d], [e, f, g]] #table must be your matrix
workbook = xlsxwriter.Workbook('excelFile.xlsx')
worksheet = workbook.add_worksheet()
col = 0
for row, data in enumerate(table):
worksheet.write_row(row, col, data)
workbook.close()
我想按第4个字段(基本上是按最后一个数字排序)将其按两行乘两行的顺序进行排序,以输出以下结果:
A 35 74 dsadasd/1 0 +
A 95 74 dsadasd/2 0 -
B 75 159 dsadasd/2 0 +
B 78 852 dsadasd/1 0 -
C 12 789 dsadasd/1 0 +
C 91 546 dsadasd/2 0 -
A 87 52 dsadasd/2 0 +
A 52 15 dsadasd/1 0 -
TIA
答案 0 :(得分:1)
awk:
$ awk '{
k=NR%2; a[k]=$4; b[k]=$0 # store compare value and
} # record for 0 and 1
!(NR%2) { # on even we compare
print b[(a[0]>a[1])] ORS b[(a[0]<=a[1])] # and print the smaller first
}' file
A 35 74 dsadasd/1 0 +
A 95 74 dsadasd/2 0 -
B 78 852 dsadasd/1 0 -
B 75 159 dsadasd/2 0 +
C 12 789 dsadasd/1 0 +
C 91 546 dsadasd/2 0 -
A 52 15 dsadasd/1 0 -
A 87 52 dsadasd/2 0 +
答案 1 :(得分:1)
应该有一种更简单的方法,但这可行
$ awk '{c+=p!=$1; p=$1; print c "\t" $0}' file | sort -k1,1 -k5 | cut -f2-
A 35 74 dsadasd/1 0 +
A 95 74 dsadasd/2 0 -
B 78 852 dsadasd/1 0 -
B 75 159 dsadasd/2 0 +
C 12 789 dsadasd/1 0 +
C 91 546 dsadasd/2 0 -
A 52 15 dsadasd/1 0 -
A 87 52 dsadasd/2 0 +
根据第一个字段组创建一个组ID,首先按其排序,然后按另一个键字段排序;删除虚拟组ID。
答案 2 :(得分:1)
尝试Perl ..请注意,这会保留输入中的空格
cloud.to_file('N.png')
带有输入
perl -0777 -ne ' while( /(.+?)\n(.+?)\n/gms ) { $a=$1;$b=$2; (split(/\s+/,$a))[3] gt (split(/\s+/,$b))[3] ? print "$b\n$a\n" : print "$a\n$b\n" }'
答案 3 :(得分:1)
awk +排序
$ awk ' { $(NF+1)=int((NR+1)/2) } 1 ' angel.txt | sort -k7,7 -k4,4 | awk ' {$NF=""}1 '
A 35 74 dsadasd/1 0 +
A 95 74 dsadasd/2 0 -
B 78 852 dsadasd/1 0 -
B 75 159 dsadasd/2 0 +
C 12 789 dsadasd/1 0 +
C 91 546 dsadasd/2 0 -
A 52 15 dsadasd/1 0 -
A 87 52 dsadasd/2 0 +
$ cat angel.txt
A 35 74 dsadasd/1 0 +
A 95 74 dsadasd/2 0 -
B 75 159 dsadasd/2 0 +
B 78 852 dsadasd/1 0 -
C 12 789 dsadasd/1 0 +
C 91 546 dsadasd/2 0 -
A 87 52 dsadasd/2 0 +
A 52 15 dsadasd/1 0 -
$
答案 4 :(得分:0)
这应该适用于awk
:
awk '{if(p==""){p=$0;p4=$4}
else{
if(p4>$4){print $0"\n"p}
else{print p"\n"$0};p=p4=""
}}' file