0

Input Name Rico

Address Australia

Age 24

Name Rica

Address Asia

Age 25

Output Name Rico, Address Australia, Age 24

Name Rica, Address Asia, Age 25

Can we do this in Unix?

peon
  • 13
  • 3
  • Yes, that would be trivial. See [ask] if you have a followup question. – Ed Morton May 15 '17 at 01:06
  • Welcome to StackOverflow. Please take the [tour], learn asking good questions stackoverflow.com/help/how-to-ask, make a [mcve]. An MCVE should include a variety of sample input (illustrating all aspects) and desired output. You should also show the effort you have invested yourself. You might have noticed that StackOverflow readers are not very happy with the impression to be used as a free coding service. – Yunnosch May 17 '17 at 05:28
  • Do you want to concat all lines into one? Your sample in and desired out seems to indicate that. You wouldn't want to only concat sets of lines "Name.../Address.../Age..." ? – Yunnosch May 17 '17 at 05:30

4 Answers4

1

with sed:

sed ':a;N;$!ba;s/\n/, /g' filename
tso
  • 4,732
  • 2
  • 22
  • 32
0

You can do converting multiple lines to single line with using awk.

#!/usr/bin/awk
# table.awk

{
    if (NR == 1) {
        printf("%s",$0);
    } else {
        printf(",%s",$0);
    }
}

$ cat input.csv

Name Rico
Address Australia
Age 24
Name Rica
Address Asia
Age 25

$ awk -f table.awk input.csv

Name Rico,Address Australia,Age 24,Name Rica,Address Asia,Age 25

There are more efficient answers in Stackoverflow, like following:

How to merge rows from the same column using unix tools

hiropon
  • 1,675
  • 2
  • 18
  • 41
0

here is another alternative...

paste -sd, inputfile
karakfa
  • 66,216
  • 7
  • 41
  • 56
0

I found the solution:

awk 'BEGIN { RS="" ; FS="\n"}; { print $1,$2 }' file

This sets the input record separator as a blank line, or consecutive blank lines for this matter, as the next record will be next non-blank line. This also sets the field separator as nextline.

peon
  • 13
  • 3