4

I have input file which is given below

Input file

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

Output which i am looking for.

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

I have tried with awk command, but i am not getting desired output. can anyone help me in this?

awk -F"" '{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}' inputfile
fedorqui
  • 275,237
  • 103
  • 548
  • 598
gyrous
  • 189
  • 3
  • 6
  • 19

7 Answers7

7

Using 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

EDIT: As commented by sudo_O and Ed Morton, the following variant is more portable:

$ 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
Community
  • 1
  • 1
devnull
  • 118,548
  • 33
  • 236
  • 227
  • 4
    +1 However you should use parenthesis with the ternary operator to avoid ambiguity *(will fail on Mac)*. I'd have written it `awk 'ORS=(NR%3?",":RS)' file` – Chris Seymour Sep 25 '13 at 10:18
  • Can someone please change their answer to `awk 'ORS=(NR%3?",":RS)' file` so I can upvote it :-). – Ed Morton Sep 25 '13 at 14:03
  • @EdMorton you known you have editing rights right? http://stackoverflow.com/help/editing – Chris Seymour Sep 25 '13 at 14:09
  • @EdMorton And your edit wouldn't need to be reviewed either :) – devnull Sep 25 '13 at 14:10
  • @sudo_O and devnull - yeah, I just hate to change the content of someone else's post, other than formatting if it's bad. It's like combing someone else's kids hair... Upvote done! – Ed Morton Sep 25 '13 at 14:16
4

With 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

With args and 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
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
4

Here is how to do it with paste:

paste -d, - - - < file

Output:

10,9:11/61432568509,118,1:/20130810014023,46,440:4/GTEL
10,9:11/61432568509,118,1:/20130810014023,46,440:4/GTEL
Thor
  • 45,082
  • 11
  • 119
  • 130
3

if each of your "data block" has 3 lines, you could do:

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

if you love awk:

awk 'NR%3{printf "%s,",$0;next}7' file
Kent
  • 189,393
  • 32
  • 233
  • 301
  • Good to see three different options! Note that the OP wants a comma `,` between the joined lines. – fedorqui Sep 25 '13 at 10:12
  • 1
    @fedorqui oh... I didn't test the 3 lines... :( I am about to fix it with comma.. thanx for point out. – Kent Sep 25 '13 at 10:17
1
> sed 'N;N;s/\n/,/g' your_file
Vijay
  • 65,327
  • 90
  • 227
  • 319
1

A short awk version

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

Shortened, thanks to iiSaymour

Jotne
  • 40,548
  • 12
  • 51
  • 55
  • 4
    Shorted still is `awk 'ORS=NR%3?",":RS' file` but like I said on @devnull's answers you really should use parenthesis around the ternary operator for portability across all awks. – Chris Seymour Sep 25 '13 at 10:33
0

One way with 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

It defines each field to be a line. Hence, it prints them on blocks of three.

fedorqui
  • 275,237
  • 103
  • 548
  • 598