How do I append a line in a file to the previous line?

I have some log files I need to process but to process them I need the two lines of the log brought together as a single line.  Lines look like this:

[Mon Oct 06 04:52:30 2008]Local/SHLEXP2///Info(1200480)
Loaded and initialized JVM module

[Mon Oct 06 04:52:30 2008]Local/SHLEXP2///Info(1200445)
External [GLOBAL] function [@ESSBASEALERT] registered OK

I need them to look like this when rewritten to a new file:
[Mon Oct 06 04:52:30 2008]Local/SHLEXP2///Info(1200480) Loaded and initialized JVM module
[Mon Oct 06 04:52:30 2008]Local/SHLEXP2///Info(1200445) External [GLOBAL] function [@ESSBASEALERT] registered OK

Can't quite figure out how I would do this.  Thanks.
Angela_WilcoxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

silemoneCommented:
just use the + symbol...

so place them in a new string and combine them using the + symbol...
0
silemoneCommented:
oops...the . is for concatenation in perl...didn't read language carefully...
0
silemoneCommented:
$string1 = "[Mon Oct 06 04:52:30 2008]Local/SHLEXP2///Info(1200480)"
$string2 = "Loaded and initialized JVM module"
$string3 = string1." ".string2


if they aren't in variables

$fullString = "[Mon Oct 06 04:52:30 2008]Local/SHLEXP2///Info(1200480)"." ". "Loaded and initialized JVM module"
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

silemoneCommented:
0
Angela_WilcoxAuthor Commented:
Thanks.  I guess I should have been more clear.  I can do it for just two lines as you indicated above.  But how do I convert an entire file?  There are 100's of string 1 string 2 sets.  How do I move from first set to next to next to next?
0
Adam314Commented:
Are your lines separated by blank lines?

$/="\n\n";
open(my $in, "<log.txt") or die "input: $!\n";
open(my $out, ">newlog.txt") or die "output: $!\n";
while(<$in>) {
	s/\n//g;
	print $out "$_\n";
}
close($in);
close($out);

Open in new window

0
silemoneCommented:
are they always separated by carriage line feed?  also will there always be a pair?
0
silemoneCommented:
it's your re...i think you could do this programmatically instead, but if you going to use re, I think you're going to have to use a more complex one...


i.e...(just the concept...i'm looking up exact syntax)

s/(.)+(\n)(.)+(\n\n)/$1$3$4/g....what i mean is you're going to have to group, find the single  \n....

0
silemoneCommented:
i think this is probably more like the syntax...

substitutes from beginning of line
at least one character followed by return carriage
followed by at least on character followed by 2 returnfields end of line  

and replaces them with first character group, third  group of characters, and double return again...only removes \n



s/^(.)+(\n)(.)+(\n\n)$/\1\3\4/g
0
silemoneCommented:
here's is a helper on this:

http://www.comp.leeds.ac.uk/Perl/sandtr.html

look up Remember Patterns
0
Adam314Commented:
silemone - I'm not sure what you are doing... do you understand the code I posted?

Here is my code again with some comments
#Set input record separator.  This means whenever you read a "line" (using <$in> below)
#perl will read until it gets this.  With this set to "\n\n", perl will read until
#it gets two newlines in a row.  In the sample file posted in the original question, #this will be the:
#  the first line of a message
#  the second line of a message
#  the blank line between messages
$/="\n\n";
 
#Open the input (original) and output (new)
open(my $in, "<log.txt") or die "input: $!\n";
open(my $out, ">newlog.txt") or die "output: $!\n";
 
#Continue while there are "lines" to read, save to $_.
#In this context a "line" is all the lines of an error message. In the sample posted
#above, this means two lines plus the blank line
while(<$in>) {
        s/\n//g;  #Remove newlines characters, meaning put all lines on 1 line
        print $out "$_\n"; print line, with a newline at end
}
 
#close files
close($in);
close($out);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
silemoneCommented:
i apologize...i didn't read thoroughly...notice $/=\n\n for first time...
0
FishMongerCommented:
If the records are not separated by blank lines and each record is 2 lines, then this could work.

while(<DATA>) {
    s/\n/ / if /^\[[a-z]+ [[a-z]+ \d+ \d+:\d+:\d+ \d+\]/i;
    print;
}
 
__DATA__
[Mon Oct 06 04:52:30 2008]Local/SHLEXP2///Info(1200480)
Loaded and initialized JVM module
[Mon Oct 06 04:52:30 2008]Local/SHLEXP2///Info(1200445)
External [GLOBAL] function [@ESSBASEALERT] registered OK

Open in new window

0
Angela_WilcoxAuthor Commented:
OK - Sorry - my internet bit the dirt - very untimely - at my local coffee house now.  Anyway - Lines always are in pairs.  Lines always are separated by a blank line.
0
Angela_WilcoxAuthor Commented:
Thanks very much for the additional comments to help me understand what it was doing - had a tiny little tweak with an extra comma - but other then that it worked great first time!  Thank you Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.