0

我有这么长的一行:

kvm1.example.corp,18:18:48,x86_64,16,11,11,0,0,0,0,0,11,0,4.6,99056376,4980736,4980736,5450000000,1,s-75-VM,460000000.,0.385304090422,262144,0,0,3,743,0,2,v-30-VM,450000000.,0.376927914543,1048576,1,0,6,7676,4765,3,r-80-VM,430000000.,0.360175562786,131072,0,0,0,208,0,5,i-2-81-VM,10000000.,0.00837617587873,1048576,1,0,0,0,0,6,r-83-VM,430000000.,0.360175562786,131072,0,0,0,156,0,7,i-3-82-VM,710000000.,0.59470848739,524288,0,0,0,0,0,8,r-85-VM,410000000.,0.343423211028,131072,0,0,0,208,0,11,i-3-84-VM,710000000.,0.59470848739,524288,0,0,0,0,0,12,i-2-79-VM,690000000.,0.577956135633,524288,0,0,2,0,0,13,r-87-VM,420000000.,0.351799386907,131072,0,0,0,156,0,14,i-3-88-VM,730000000.,0.611460839147,524288,0,0,2,0,0

我正在尝试获得这样的输出

1,s-75-VM,460000000.,0.385304090422,262144,0,0,3,743,0,
2,v-30-VM,450000000.,0.376927914543,1048576,1,0,6,7676,4765,
3,r-80-VM,430000000.,0.360175562786,131072,0,0,0,208,0,
5,i-2-81-VM,10000000.,0.00837617587873,1048576,1,0,0,0,0,
6,r-83-VM,430000000.,0.360175562786,131072,0,0,0,156,0,
7,i-3-82-VM,710000000.,0.59470848739,524288,0,0,0,0,0,
8,r-85-VM,410000000.,0.343423211028,131072,0,0,0,208,0,
11,i-3-84-VM,710000000.,0.59470848739,524288,0,0,0,0,0,
12,i-2-79-VM,690000000.,0.577956135633,524288,0,0,2,0,0,
13,r-87-VM,420000000.,0.351799386907,131072,0,0,0,156,0,
14,i-3-88-VM,730000000.,0.611460839147,524288,0,0,2,0,0

我不确定在删除标题后应该采取哪种策略在第 9 个逗号后添加新行?

4

4 回答 4

2
awk -F, '{for (col = 19; col < NF; col += 10) {
           for (i = 0; i < 10; i++) { printf("%s,", $(col+i)); }
           printf "\n"; }
         }'
于 2013-02-13T18:38:17.390 回答
2

更简单sed

$ sed -r 's/([^,]+,){18}//;s/(([^,]+,){10})/\1\n/g' file 
1,s-75-VM,460000000.,0.385304090422,262144,0,0,3,743,0,
2,v-30-VM,450000000.,0.376927914543,1048576,1,0,6,7676,4765,
3,r-80-VM,430000000.,0.360175562786,131072,0,0,0,208,0,
5,i-2-81-VM,10000000.,0.00837617587873,1048576,1,0,0,0,0,
6,r-83-VM,430000000.,0.360175562786,131072,0,0,0,156,0,
7,i-3-82-VM,710000000.,0.59470848739,524288,0,0,0,0,0,
8,r-85-VM,410000000.,0.343423211028,131072,0,0,0,208,0,
11,i-3-84-VM,710000000.,0.59470848739,524288,0,0,0,0,0,
12,i-2-79-VM,690000000.,0.577956135633,524288,0,0,2,0,0,
13,r-87-VM,420000000.,0.351799386907,131072,0,0,0,156,0,
14,i-3-88-VM,730000000.,0.611460839147,524288,0,0,2,0,0
于 2013-02-13T18:52:51.080 回答
2

在 bash 中:

(
  IFS="," read -a fields <<< "$longline"
  set -- "${fields[@]:18}"
  IFS=","
  while (( $# > 0 )); do
    echo "${*:1:10}"
    shift 10
  done
)

创建一个子shell ,(...)这样我们就不必担心保留IFSshell参数($@$*)的旧值。

第一行使用“,”作为字段分隔符分割长行,并将字段存储在数组中。

第二行跳过前 18 个字段(所有标题元素)并将其余部分存储在 shell 参数中,这些参数可以使用 , 等单独访问,也可以$1作为$2or 集体$@访问$*

第三行为 while 循环的其余部分设置字段分隔符“,”,特别是用于echo命令中的参数扩展。

"${*:1:10}"接受前 10 个 shell 参数,并将它们与IFS.

shift 10 丢弃前 10 个参数(我们刚刚打印的)。

$#shift是 shell 参数的数量,每次操作后减少 10 ;只要还有要处理的参数,循环就会继续。

于 2013-02-13T19:26:59.787 回答
1

在 perl 中:

perl -F, -lane 'for($min=18;$min<scalar(@F);$min+=10){print join ",",@F[$min..$min+10]}'

测试如下:

> echo "kvm1.example.corp,18:18:48,x86_64,16,11,11,0,0,0,0,0,11,0,4.6,99056376,4980736,4980736,5450000000,1,s-75-VM,460000000.,0.385304090422,262144,0,0,3,743,0,2,v-30-VM,450000000.,0.376927914543,1048576,1,0,6,7676,4765,3,r-80-VM,430000000.,0.360175562786,131072,0,0,0,208,0,5,i-2-81-VM,10000000.,0.00837617587873,1048576,1,0,0,0,0,6,r-83-VM,430000000.,0.360175562786,131072,0,0,0,156,0,7,i-3-82-VM,710000000.,0.59470848739,524288,0,0,0,0,0,8,r-85-VM,410000000.,0.343423211028,131072,0,0,0,208,0,11,i-3-84-VM,710000000.,0.59470848739,524288,0,0,0,0,0,12,i-2-79-VM,690000000.,0.577956135633,524288,0,0,2,0,0,13,r-87-VM,420000000.,0.351799386907,131072,0,0,0,156,0,14,i-3-88-VM,730000000.,0.611460839147,524288,0,0,2,0,0" | perl -F, -lane 'for($min=18;$min<scalar(@F);$min+=10){print join ",",@F[$min..$min+10]}' 
1,s-75-VM,460000000.,0.385304090422,262144,0,0,3,743,0,2
2,v-30-VM,450000000.,0.376927914543,1048576,1,0,6,7676,4765,3
3,r-80-VM,430000000.,0.360175562786,131072,0,0,0,208,0,5
5,i-2-81-VM,10000000.,0.00837617587873,1048576,1,0,0,0,0,6
6,r-83-VM,430000000.,0.360175562786,131072,0,0,0,156,0,7
7,i-3-82-VM,710000000.,0.59470848739,524288,0,0,0,0,0,8
8,r-85-VM,410000000.,0.343423211028,131072,0,0,0,208,0,11
11,i-3-84-VM,710000000.,0.59470848739,524288,0,0,0,0,0,12
12,i-2-79-VM,690000000.,0.577956135633,524288,0,0,2,0,0,13
13,r-87-VM,420000000.,0.351799386907,131072,0,0,0,156,0,14
14,i-3-88-VM,730000000.,0.611460839147,524288,0,0,2,0,0,
于 2013-02-14T12:10:40.603 回答