?
Solved

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

Posted on 2008-11-03
15
Medium Priority
?
299 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:Angela_Wilcox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 3
  • 2
  • +1
15 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 22867544
just use the + symbol...

so place them in a new string and combine them using the + symbol...
0
 
LVL 21

Expert Comment

by:silemone
ID: 22867552
oops...the . is for concatenation in perl...didn't read language carefully...
0
 
LVL 21

Expert Comment

by:silemone
ID: 22867569
$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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 21

Expert Comment

by:silemone
ID: 22867583
0
 

Author Comment

by:Angela_Wilcox
ID: 22867618
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
 
LVL 39

Expert Comment

by:Adam314
ID: 22867620
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
 
LVL 21

Expert Comment

by:silemone
ID: 22867774
are they always separated by carriage line feed?  also will there always be a pair?
0
 
LVL 21

Expert Comment

by:silemone
ID: 22867898
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
 
LVL 21

Expert Comment

by:silemone
ID: 22867960
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
 
LVL 21

Expert Comment

by:silemone
ID: 22867992
here's is a helper on this:

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

look up Remember Patterns
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 2000 total points
ID: 22868378
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
 
LVL 21

Expert Comment

by:silemone
ID: 22868440
i apologize...i didn't read thoroughly...notice $/=\n\n for first time...
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 22868724
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
 

Author Comment

by:Angela_Wilcox
ID: 22869313
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
 

Author Closing Comment

by:Angela_Wilcox
ID: 31512709
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question