4

我有下面给出的输入文件

输入文件

10,9:11/61432568509
118,1:/20130810014023
46,440:4/GTEL
10,9:11/61432568509
118,1:/20130810014023
46,440:4/GTEL

我正在寻找的输出。

10,9:11/61432568509,118,1:/20130810014023,46,440:4/GTEL
10,9:11/61432568509,118,1:/20130810014023,46,440:4/GTEL

我尝试过使用 awk 命令,但没有得到想要的输出。任何人都可以帮助我吗?

awk -F"" '{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}' inputfile
4

7 回答 7

7

使用awk

$ awk 'ORS=(NR%3==0)?"\n":","' inputfile
10,9:11/61432568509,118,1:/20130810014023,46,440:4/GTEL
10,9:11/61432568509,118,1:/20130810014023,46,440:4/GTEL

编辑:正如sudo_OEd Morton所评论的,以下变体更便携

$ awk 'ORS=(NR%3?",":RS)' inputfile
10,9:11/61432568509,118,1:/20130810014023,46,440:4/GTEL
10,9:11/61432568509,118,1:/20130810014023,46,440:4/GTEL
于 2013-09-25T09:52:51.770 回答
4

以下是如何做到这一点paste

paste -d, - - - < file

输出:

10,9:11/61432568509,118,1:/20130810014023,46,440:4/GTEL
10,9:11/61432568509,118,1:/20130810014023,46,440:4/GTEL
于 2013-09-25T11:20:49.397 回答
4

pr

$ pr -ats, file --columns 3  
10,9:11/61432568509,118,1:/20130810014023,46,440:4/GTEL
10,9:11/61432568509,118,1:/20130810014023,46,440:4/GTEL

argstr:_

$ xargs -n3 < file | tr ' ' ,
10,9:11/61432568509,118,1:/20130810014023,46,440:4/GTEL
10,9:11/61432568509,118,1:/20130810014023,46,440:4/GTEL
于 2013-09-25T10:13:01.847 回答
3

如果您的每个“数据块”有 3 行,您可以这样做:

sed -n 'N;N;s/\n/,/g;p' file

如果你喜欢 awk:

awk 'NR%3{printf "%s,",$0;next}7' file
于 2013-09-25T09:46:31.657 回答
1

一个简短的awk版本

awk 'ORS=NR%3?",":RS' file

缩短,感谢 iiSaymour

于 2013-09-25T10:20:10.700 回答
1
> sed 'N;N;s/\n/,/g' your_file
于 2013-09-25T09:49:10.403 回答
0

使用 awk 的一种方法:

$ awk -v RS= -F'\n' 'BEGIN{OFS=","}{for (i=1;i<=NF; i=i+3) {print $i,$(i+1),$(i+2)}}' file
10,9:11/61432568509,118,1:/20130810014023,46,440:4/GTEL
10,9:11/61432568509,118,1:/20130810014023,46,440:4/GTEL

它将每个字段定义为一行。因此,它将它们打印在三个块上。

于 2013-09-25T09:46:41.030 回答