如何使用awk在新行上打印每四个字段?

时间:2017-01-04 17:46:27

标签: r awk

当我将大尺寸R输出导出到文本文件时,它会扭曲输出的外观。所以我需要正确地导出它,因为它看起来在R中输出。你能给我一个解决方案吗?

以下数据如下:

4 546.5 1 0.995012  
4 542.5 2 0.990050
4 539 3 0.985112
4 535 4 0.980199
10 488 1 0.995012
10 490.5 2 0.990050
10 470.5 3 0.985112
10 472 4 0.980199

我想要拟合单独的模型,并使用$ 2作为响应变量获得每个ID($ 1)的回归系数,使用$ 3和$ 4作为解释变量。
我得到了这样的输出:

Call:
   Model: weight ~ dim + expdim | id 
   Data: sample 

 Coefficients:
   (Intercept) 
    Estimate Std. Error     t value  Pr(>|t|)
4    633.5365   284123.2 0.002229795 0.9984233
10 17237.8328   284123.2 0.060670276 0.9571391
   dim 
    Estimate Std. Error      t value  Pr(>|t|)
4   -4.211269   1403.006 -0.003001604 0.9978776
10 -89.465142   1403.006 -0.063766745 0.9549559
  expdim 
      Estimate Std. Error       t value  Pr(>|t|)
4     -83.29169     284141 -0.0002931351 0.9997927
10 -16741.62918     284141 -0.0589201546 0.9583733

   Residual standard error: 6.961743 on 2 degrees of freedom

我使用以下代码导出输出但是它扭曲了它。

output <- capture.output(summary(m2))
cat("My title", output, file="m2.txt", sep="n", append=TRUE)

1 个答案:

答案 0 :(得分:1)

使用xargs

拥有此文件:

$ cat m2.txt
538.8915   7.514458  71.71395  0.000000e+00n10    482.6413   7.782459  
62.01655  0.000000e+00n22    523.8301   8.799187  59.53165  0.000000e+00n27

<强>结果

$ xargs -n4 <m2.txt
538.8915 7.514458 71.71395 0.000000e+00n10
482.6413 7.782459 62.01655 0.000000e+00n22
523.8301 8.799187 59.53165 0.000000e+00n27

来自man页面:

 -n number
         Set the maximum number of arguments taken from standard input for each invocation of utility.
相关问题