我有下面给出的input文件
input文件
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
使用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_O和埃德·莫顿评论,下面的变种更便携 :
$ 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
用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
用args
和tr
:
$ 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
这里是如何做到这一点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
如果你的每个“数据块”有3行,你可以这样做:
sed -n 'N;N;s/\n/,/g;p' file
如果你爱awk:
awk 'NR%3{printf "%s,",$0;next}7' file
> sed 'N;N;s/\n/,/g' your_file
一个简短的awk
版本
awk 'ORS=NR%3?",":RS' file
缩短,感谢iiSaymour
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
它将每个字段定义为一行。 因此,它将以三个块为单位进行打印。