我有这么长的路线:
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个逗号后添加新行应该采用哪种策略?
答案 0 :(得分:2)
awk -F, '{for (col = 19; col < NF; col += 10) {
for (i = 0; i < 10; i++) { printf("%s,", $(col+i)); }
printf "\n"; }
}'
答案 1 :(得分: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
答案 2 :(得分:2)
在bash中:
(
IFS="," read -a fields <<< "$longline"
set -- "${fields[@]:18}"
IFS=","
while (( $# > 0 )); do
echo "${*:1:10}"
shift 10
done
)
(...)
创建一个子shell,这样我们就不必担心保留IFS
的旧值和shell参数($@
和$*
)。
第一行使用','作为字段分隔符拆分长行,并将字段存储在数组中。
第二行跳过前18个字段(所有标题元素)并将其余字段存储在shell参数中,可以使用$1
,$2
等单独访问,也可以共同{{1 }或$@
。
第三行为while循环的其余部分创建字段分隔符',',特别是$*
命令中的参数扩展。
echo
获取前10个shell参数,并使用"${*:1:10}"
的值连接它们。
shift 10丢弃前10个参数(我们刚刚打印)。
IFS
是shell参数的数量,在每次$#
操作后减少10;只要还有要处理的参数,循环就会继续。
答案 3 :(得分:1)
in 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,