Row to column transpose between same pattern.

Hi All,

I have been trying to transpose rows to column in an large file (about 15000 lines) between matching pattern. Searched all posts in this forum, but not able find the solution to my problem. Any help appreciated.!!

Input

/*------XXXXXX-------*/
 
owner: XXXX
location: XXXX
Comment: XXXX
Phone: XXXX
 
/*------XXXXXX-------*/
 
owner: XXXX
location: XXXX
Desc: XXXX
Comment: XXXX
Phone: XXXX
Log: XXXX
 
/*------XXXXXX-------*/
Test: XXXX
Site: XXXX
Area: XXX

My desired output is

/*------XXXXXX-------*/^owner: XXXX^location: XXXX^Comment: XXXX^Phone: XXXX
/*------XXXXXX-------*/^owner: XXXX^location: XXXX^Desc: XXXX^Comment: XXXX^Phone: XXXX^Log: XXXX
/*------XXXXXX-------*/^Test: XXXX^Site: XXXX^Area: XXX

Match pattern "/*------" and then join all the line into single line, before next appearance of same pattern.

Thanks in advance.

Try...

awk '/\/\*------/{if(b)print b;b=$0;next}NF{b=b "^" $0}END{print b}' file1 > file2
2 Likes

Hi RobP,

One way using perl:

$ cat infile
/*------XXXXXX-------*/

owner: XXXX
location: XXXX
Comment: XXXX
Phone: XXXX

/*------XXXXXX-------*/

owner: XXXX
location: XXXX
Desc: XXXX
Comment: XXXX
Phone: XXXX
Log: XXXX

/*------XXXXXX-------*/
Test: XXXX
Site: XXXX
Area: XXX
$ cat script.pl
use strict;
use warnings;

my @data;

while ( <> ) {
        next unless m/\S/;
        chomp;

        if ( m{\A/\*-+} ) {
                printf qq[%s\n], join qq[^], splice @data if ( @data );
                push @data, $_;
                next;
        }

        push @data, $_;

        if ( eof && @data ) {
                printf qq[%s\n], join qq[^], splice @data;
        }
}
$ perl script.pl infile
/*------XXXXXX-------*/^owner: XXXX^location: XXXX^Comment: XXXX^Phone: XXXX
/*------XXXXXX-------*/^owner: XXXX^location: XXXX^Desc: XXXX^Comment: XXXX^Phone: XXXX^Log: XXXX
/*------XXXXXX-------*/^Test: XXXX^Site: XXXX^Area: XXX
1 Like

Hi Ygor / birei,

Thank you both for quick help. I have tried both awk code and perl script. For my example file both the codes working fine, however when I am using the same code (awk) in my original file all the lines joined to single line instead a new line to start with matching pattern, and with perl script there is no output at all. May be something is wrong in the example I used.