如何插入两行用awk每个数据帧? [英] How to insert two lines for every data frame using awk?

查看:147
本文介绍了如何插入两行用awk每个数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有重复的数据如下:

....
 4 4 4 66 79 169 150 0  40928  40938  40923  40921  40789  40000  40498
 5 4 3 16 22 247 0  40168  40911  40944  40205  40000  40562
 6 4 4 17 154 93 309 0  40930  40919  40903  40917  40852  40000  40419
 7 3 2 233 311 0  40936  40932  40874  40000  40807
....

此数据由115的数据块,每个数据块具有4000行类似的格式。
 在这里,我希望把两条新线在每个数据块的开头(每个数据块= 4000和空行的行数),所以看起来

This data is made up of 115 data blocks, and each data block have 4000 lines like that format. Here, I hope to put two new lines (number of line per data block = 4000 and empty line) at the begining of each data blocks, so it looks

4000

 1 4 4 244 263 704 952 0  40936  40930  40934  40921  40820  40000  40570
 2 4 4 215 172 305 33 0  40945  40942  40937  40580  40687  40000  40410
 3 4 4 344 279 377 1945 0  40933  40915  40907  40921  40839  40000  40437
 4 4 4 66 79 169 150 0  40928  40938  40923  40921  40789  40000  40498
...
 3999 2 2 4079 4081 0  40873  40873  40746  40000  40634
 4000 1 1 4080 0  40873  40923  40000  40345
4000

 1 4 4 244 263 704 952 0  40936  40930  40934  40921  40820  40000  40570
 2 4 4 215 172 305 33 0  40945  40942  40937  40580  40687  40000  40410
 3 4 4 344 279 377 1945 0  40933  40915  40907  40921  40839  40000  40437
 4 4 4 66 79 169 150 0  40928  40938  40923  40921  40789  40000  40498
... 

我可以做到这一点使用awk或任何其他UNIX命令?

Can I do this with awk or any other unix command?

推荐答案

用一个简单的单行 AWK 可以做的目的。

A simple one liner using awk can do the purpose.

的awk'NR%4000 == 1 {打印4000 \\ n} {$打印0}文件

它做什么。

打印$ 1,0 打印每行。

NR%4000 == 1
选择 4000 日线。当它occures它打印 4000 和换行符 \\ n ,也就是两条新线。

print $0 prints every line. NR%4000==1 selects the 4000th line. When it occures it prints a 4000 and a newline \n, that is two new lines.

NR 记录数量,这是行数用途不同,到目前为止读取。

NR Number of records, which is effectivly number of lines reads so far.

简单的测试。

在5日线插入4000

inserts 4000 at 5th line

的awk'NR%5 == 1 {打印4000 \\ n} {$打印0}

输出:

4000

1
2
3
4
5
4000

6
7
8
9
10
4000

11
12
13
14
15
4000

16
17
18
19
20
4000

这篇关于如何插入两行用awk每个数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆