Solved

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

Posted on 2008-11-03
15
291 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl rename 2 142
binary to char / hexadecimal 5 104
SIMPLE Perl Regex 1 153
Perl Untar File 1 25
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.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now