我是编程的初学者,所以这个问题可能很简单。
我想来自:
1 sth
2 sth
3 sth
4 sth
3 sth
2 sth
2 sth
要:
1 sth
2 sth
2 sth
2 sth
3 sth
3 sth
4 sth
这是热图的gnuplot所需的格式。我知道你可以使用sort -n "datafile"
按行中的第一个数字对数据进行排序,但是如何将所有内容拆分成块?感谢。
答案 0 :(得分:4)
这种做法:
$ sort -n file | awk '{if (a!=$1) {print ""} a=$1}1'
1 sth
2 sth
2 sth
2 sth
3 sth
3 sth
4 sth
sort -n file
很明显,因为您已经使用过它。awk '{if (a!=$1) {print ""} a=$1}1'
$1
中保存第一个字段值(a
)来实现的。{}1
条件打印每一行。答案 1 :(得分:2)
Perl解决方案:
sort -n datafile | perl -ape 'print "\n" if defined $previous and $F[0] != $previous; $previous = $F[0]'