我有输入文件,如下所示
输入文件
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
答案 0 :(得分: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_O和Ed 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
答案 1 :(得分: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
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
答案 2 :(得分: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
答案 3 :(得分:3)
如果你的每个“数据块”都有3行,你可以这样做:
sed -n 'N;N;s/\n/,/g;p' file
如果你喜欢awk:
awk 'NR%3{printf "%s,",$0;next}7' file
答案 4 :(得分:1)
> sed 'N;N;s/\n/,/g' your_file
答案 5 :(得分:1)
简短的awk
版本
awk 'ORS=NR%3?",":RS' file
缩短,感谢iiSaymour
答案 6 :(得分: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
它将每个字段定义为一条线。因此,它将它们打印在三个块上。