Converting columns of text to rows, with blank lines

I've spent the past hour trying different things and googling for this solution and cannot find the answer. Found variations of this, but not this exact thing.

I have the following text, which is the output from our mainframe. Each field is on a separate line, with a blank line between each record. I want to the lines together. A blank line indicates a new row. Note that some of the fields contain slashes, periods, and other punctuation. Also the number of lines to join is variable, but records are always separated by a single blank line.

This is the contents of the source text file:

7LKZB88901
DC090SDFKGHSDD
REJECT REASON DDP/09/Q
SCC/GO/SEX.M.DNY/ALASKA/ CONTACT THE DENYING AGENCY
For the following
DUPLICATE

7LKZB88902
DC090RDFK1HSDD
PENDING

8PKZB88903
DC0901DFK1HSD7
ACCEPT

7LKZB88901
DC090SDFKGHSDD
REJECT REASON DDP/09/Q
SCC/GO/SEX.M.DNY/ALASKA/ CONTACT THE DENYING AGENCY
For the following
ERROR

5KZZB88912
DC0901DFF1HGG2
ACCEPT

And here's what I want the output to look like:

7LKZB88901 DC090SDFKGHSDD REJECT REASON DDP/09/Q SCC/GO/SEX.M.DNY/ALASKA/ CONTACT THE DENYING AGENCY For the following DUPLICATE
7LKZB88902 DC090RDFK1HSDD PENDING
8PKZB88903 DC0901DFK1HSD7 ACCEPT
7LKZB88901 DC090SDFKGHSDD REJECT REASON DDP/09/Q SCC/GO/SEX.M.DNY/ALASKA/ CONTACT THE DENYING AGENCY For the following ERROR
5KZ4B88912 DC0901DFF1HGG2 ACCEPT

My script foo isn't powerful enough. :frowning: Any help is appreciated with this one. :b:

How about

awk '$1=$1' OFS=" " RS= file
7LKZB88901 DC090SDFKGHSDD REJECT REASON DDP/09/Q SCC/GO/SEX.M.DNY/ALASKA/ CONTACT THE DENYING AGENCY For the following DUPLICATE
7LKZB88902 DC090RDFK1HSDD ACCEPT
8PKZB88903 DC0901DFK1HSD7 ACCEPT
7LKZB88901 DC090SDFKGHSDD REJECT REASON DDP/09/Q SCC/GO/SEX.M.DNY/ALASKA/ CONTACT THE DENYING AGENCY For the following DUPLICATE
5KZZB88912 DC0901DFF1HGG2 ACCEPT

Might not work with all awk version, pls. post yours.

1 Like

That didn't work for me, it concatenated all records into one single row, rather than each record into its own row.

This is a RHEL 6 server, the awk is GNU awk v3.1.7

Thanks

Try RS="" and / or RS="\n\n" .

1 Like

Hmm..

The first one removes the blank lines, but does not concatenate any lines.

The second one concatenates all lines from all records into a one single row.

awk '$1=$1' RS= infile
1 Like

Yikes, ok, I just realized it's not a blank line in between records. It's a line containing a single " " space character.

I have deleted all of those single-spaces, so the blank lines are truly blank lines, and the solution worked.

thank you!

awk '$1=$1' RS="\n^ *$\n" infile
2 Likes