jefrey cayab
jefrey cayab

Reputation: 33

Merge multiple lines

I have a file which contains multiple like this:

s10123-yyy.bkp.abc01.zone,Windows File =
System,N/A,defaultBackupSet,default,272188(* )(S =
),Completed,INCR,Mixed,02/28/2015 19:00:27,02/28/2015 =
19:03:06,02/28/2015 20:32:11,02/28/2015 =
20:32:09,12.08,53.93%,0.18,98.52%,0%,0.12,1:28:23,N/A,8.203,N/A,67303,0,8=
3,"Disk_Library2, Disk_Library6,",N/A,N/A,=0A=

Which I need to make it in one line like this:

s10123-yyy.bkp.abc01.zone,Windows File System,N/A,defaultBackupSet,default,272188(* )(S ),Completed,INCR,Mixed,02/28/2015 19:00:27,02/28/2015 19:03:06,02/28/2015 20:32:11,02/28/2015 20:32:09,12.08,53.93%,0.18,98.52%,0%,0.12,1:28:23,N/A,8.203,N/A,67303,0,83,"Disk_Library2, Disk_Library6,",N/A,N/A

If I do it manually, I highlight the "=" and press "delete" button twice to connect and get the desired result. The last 5 character ",=0A=" needs to be deleted too.

Awk, Sed, Bash, Perl or Python script would be preferred. Appreciate you help.

Thanks!

Upvotes: 0

Views: 77

Answers (5)

glenn jackman
glenn jackman

Reputation: 246827

sed

sed '
    :a
    /,=0A=$/ {s///; s/\n//g} # "end of line", remove the chars and newlines
    / \?=$/ {s///; N; ba}    # line continuation: remove the chars, append
                             # the next line, goto a
' file

Upvotes: 0

TLP
TLP

Reputation: 67908

This is a Perl solution:

perl -l -0777 -pwe"s/,?=(?:0A=)?\n//g" file
  • -0777 disables input record separator, making the file into one single line.
  • -p reads input from file and prints it back to standard output.
  • -l (before -0) adds newline to your print statements.
  • The regex s/,?=(?:0A=)?\n//g finds an optional comma, followed by =, followed by optional 0A= string, and ending with newline.

I don't know if all your files are just one of these long lines. If it is multiple such lines, you should set the input record separator to =0A=\n, most likely, chomp the lines and delete =\n.

Upvotes: 0

Wintermute
Wintermute

Reputation: 44043

This is most simple with awk1:

awk -v RS=',=0A=\n' -F '=\n' -v OFS= '{ $1 = $1 } 1' filename

The trick is to

  • use ,=0A=\n as record separator RS
  • =\n as field separator
  • have an empty output field separator OFS, so that the fields are printed directly one after the other, and
  • force the rebuilding of the output record with $1 = $1 before printing it.

Addendum: Obligatory crazy sed solution:

sed -n '/,=0A=$/ { s///; H; s/.*//; x; s/\n//g; p; d; }; /=$/ { s///; H; }' filename

I don't recommend that you use that; I just like writing things in sed that shouldn't be written in sed. It's fun!

1 Tested with GNU awk and mawk, which are the most common ones. Multi-character RS is not strictly required by POSIX, though, so more esoteric awks may reject this. Thanks to @TomFenech for pointing this out.

Upvotes: 1

RustyShackleford
RustyShackleford

Reputation: 27328

In Python, Create a list and then use the extend method to add the lines to the list, versus append.

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174706

Through Perl.

perl -0777pe 's/=\n|,=[^,]*$//sg' file

Upvotes: 0

Related Questions