首页 > 解决方案 > 如何拆分一行但在 bash 中保留其余行?

问题描述

我有这个:

2018-08-18 1 2 0.335106 0.335106
2018-08-19 1 0.335106
2018-08-20 1 0.335106

我要这个:

2018-08-18 1 0.335106 
2018-08-18 2 0.335106
2018-08-19 1 0.335106
2018-08-20 1 0.335106

最后,我也需要摆脱 1 和 2。

到目前为止,我只能摆脱 1 和 2,但是当我这样做时,结果是:

2018-08-18 2
2018-08-19 1 0.335106
2018-08-20 1 0.335106

感谢您的帮助。

现在,我有这个:

if [ $elt_btch | wc -w ] -gt 1 then       
     size_eout_rec=`$last_7th_day $elt_btch  $filesiz`
     awk '{ 
        wrdcnt = (NF - 1)/2
        for (i=1; i<=wrdcnt; i++) 
            print $1, $(i+1), $(i+1+wrdcnt) >> $size_out_arc
          }' <<END
    else if [ $elt_btch | wc -w ] -eq 1 then    
            echo $last_7th_day $elt_btch  $filesiz >> $size_out_arc

         fi
   if 

标签: bash

解决方案


一般解决方案:

awk '{ 
    half = (NF - 1)/2
    for (i=1; i<=half; i++) 
        print $1, $(i+1), $(i+1+half)
}' <<END
2018-08-18 1 2 0.335106 0.335106
2018-08-19 1 0.335106
2018-08-20 1 0.335106
1970-01-01 1 2 3 4 0.1 0.2 0.3 0.4
END
2018-08-18 1 0.335106
2018-08-18 2 0.335106
2018-08-19 1 0.335106
2018-08-20 1 0.335106
1970-01-01 1 0.1
1970-01-01 2 0.2
1970-01-01 3 0.3
1970-01-01 4 0.4

不确定您所说的“摆脱 1 和 2”是什么意思,但您可能想要

print $1, $(i+1+half)

省略打印第二列


推荐阅读