在以模式开头的行中插入换行符


0

我有一个巨大的文件,具有以下格式:

#pair:  0   1   0   1   0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
0   0   1   
#pair:  1   2   0   1   1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
1   0   0   
#pair:  1   2   1   2   0   0   1

我想找到以“#pair:”开头的每一行,并将最后3列直接移到该行下方。例如:

#pair:  1   2   1   2   0   0   1

更改为:

#pair:  1   2   1   2   
0   0   1

如何使用在Linux中运行的命令?


那些是每个分隔3个空格,还是标签?或者那是无关紧要的?
slhck 2014年

Answers:


1

awk有点冗长:

awk '
    $1=="#pair:" {
        sep=""
        for (i=1; i<NF-2; i++) {
            printf "%s%s", sep, $i
            sep="\t"
        }
        print ""
        printf "%s\t%s\t%s\n", $(NF-2), $(NF-1), $NF
        next
    }
    1
' <<END
#pair:  0       1       2       3       4       5       6
0       0       1
#pair:  0       1       2       31      4       5       6
0       0       1
END
#pair:  0       1       2       3
4       5       6
0       0       1
#pair:  0       1       2       31
4       5       6
0       0       1

1

你可以sed这样说:

sed '/^#/ s/'$'\t''\([0-9]'$'\t''[0-9]'$'\t''[0-9]\)$/\n\1/'
     ^    ^    ^     ^                                ^ ^
     |    |    |     |                                | |
     |  replace|   digit                              | What was remembered
line starts   tab                                     | in the 1st \(...\)
with a #                                           newline
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.