Solved

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

Posted on 2008-11-03
15
290 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
Comment Utility
just use the + symbol...

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

Expert Comment

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

Expert Comment

by:silemone
Comment Utility
$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
Comment Utility
0
 

Author Comment

by:Angela_Wilcox
Comment Utility
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
Comment Utility
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
Comment Utility
are they always separated by carriage line feed?  also will there always be a pair?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 21

Expert Comment

by:silemone
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
i apologize...i didn't read thoroughly...notice $/=\n\n for first time...
0
 
LVL 28

Expert Comment

by:FishMonger
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

11 Experts available now in Live!

Get 1:1 Help Now