kar_thik82 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I have two files the file1.txt and file2.txt. Both fi1es contains huge text messages with spaces and empty lines. I want to merge the two files has single text file seperated by newline. Below are the sample text for both Files and the expected output.

File1.txt

AD##Windows AD Event Log error-EventID:1001-Fault bucket , type 0 Event Name: MpTelemetry Response: Not available Cab Id: 0 Problem signature: P1: 0x80070490 P2: PackagesNotApplicable P3: unspecified P4: 11.1.4406.0 P5: mpsigstub.exe P6: unspecified P7: unspecified P8: P9: P10: Attached files: C:\Windows\Temp\MpSigStub.log C:\Windows\Temp\MPTelemetrySubmit\client_manifest.txt These files may be available here: C:\ProgramData\Microsoft\Windows\WER\ReportQueue\NonCritical_0x8007049 +0_d01184651bd335ae784ced19edd07fd2867bdcd6_cab_11b9d51d

file2.txt

Common-Sys##Windows error-EventID:7036-LiveUpdate stopped Common-Sys##Windows error-EventID:7036-DNS Client running Common-Sys##Windows error-EventID:7036-LiveUpdate running Common-Sys##Windows error-EventID:7036-WinHTTP Web Proxy Auto-Discover +y Service running AD##Windows AD Event Log error-EventID:5719-This computer was not able + to set up a secure session with a domain controller in domain GDNINDIA due to the following: There are currently no logon servers available to service the logon re +quest. This may lead to authentication problems. Make sure that this computer is connected to the network. If the problem persists, please contact your domain administrator. Analysis symbol: Rechecking for solution: 0 Report Id: 66445275-c461-11e2-8735-002511a1e59c Report Status: 36

Expected Output

AD##Windows AD Event Log error-EventID:1001-Fault bucket , type 0 Even +t Name: MpTelemetryResponse: Not available Cab Id: 0 Problem signatur +e:P1: 0x80070490P2: PackagesNotApplicableP3: unspecifiedP4: 11.1.4406 +.0P5: mpsigstub.exeP6: unspecifiedP7: unspecifiedP8: P9: P10:Attached + files:C:\Windows\Temp\MpSigStub.log C:\Windows\Temp\MPTelemetrySubmi +t\client_manifest.txt These files may be available here:C:\ProgramDat +a\Microsoft\Windows\WER\ReportQueue\NonCritical_0x80070490_d01184651b +d335ae784ced19edd07fd2867bdcd6_cab_11b9d51d Analysis symbol: Rechecki +ng for solution: 0 Report Id: 66445275-c461-11e2-8735-002511a1e59c Re +port Status: 36 Common-Sys##Windows error-EventID:7036-LiveUpdate stopped Common-Sys##Windows error-EventID:7036-DNS Client running Common-Sys##Windows error-EventID:7036-LiveUpdate running Common-Sys##Windows error-EventID:7036-WinHTTP Web Proxy Auto-Discover +y Service running AD##Windows AD Event Log error-EventID:5719-This computer was not able + to set up a secure session with a domain controller in domain GDNINDIA due to the following: There are currentl +y no logon servers available to service the logon request.This may le +ad to authentication problems. Make sure that this computer is connec +ted to the network. If the problem persists,please contact your domai +n administrator.
use File::Slurp; my $outputfile1 = "file1.txt" my $outputfile2 = "file2.txt" my $outputfile3 = "file3.txt" open (my $combined_file,">$outputfile3") or die "cannot create combine +d output file - $!"; my @a = read_file($outputfile1) or die "couldn't read $outputf +ile1 - $! [$^E]"; my @b = read_file($outputfile2) or die "couldn't read $outputfile2 + - $! [$^E]"; my $combined = {}; # hashref my $i=0; foreach (@a) { chomp $_; $combined->{$i}{b} = '' unless defined $combined->{$i}{b}; $combined->{$i++}{a} = $_; } $i=0; foreach (@b) { chomp $_; $combined->{$i}{a} = '' unless defined $combined->{$i}{a}; $combined->{$i++}{b} = $_; } foreach my $i (sort {$a<=>$b} keys %$combined) { print $combined_file $combined->{$i}{a}, ("\n"),$combined->{$i +}{b}; } } close($combined_file);

Current Output

D##Windows AD Event Log error-EventID:1001-Fault bucket , type 0 Common-Sys##Windows error-EventID:7036-Multimedia Class Scheduler runn +ing Event Name: MpTelemetry Common-Sys##Windows error-EventID:7036-WinHTTP Web Proxy Auto-Discover +y Service stopped Response: Not available Common-Sys##Windows error-EventID:7036-Application Experience stopped Cab Id: 0 Common-Sys##Windows error-EventID:35-The time service is now synchroni +zing the system time with the time source bg6dc002.GDNINDIA.COM (ntp. +d|0.0.0.0:123->10.116.189.13:123). Problem signature: IIS##Windows IIS Event Log error-EventID:37-The time provider NtpClien +t is currently receiving valid time data from bg6dc002.GDNINDIA.COM ( +ntp.d|0.0.0.0:123->10.116.189.13:123). P1: 0x80070490 P2: PackagesNotApplicable Common-Sys##Windows error-EventID:7036-Multimedia Class Scheduler stop +ped P3: unspecified Common-Sys##Windows error-EventID:7036-Adobe Flash Player Update Servi +ce stopped P4: 11.1.4406.0 Common-Sys##Windows error-EventID:7036-Adobe Flash Player Update Servi +ce running P5: mpsigstub.exe Common-Sys##Windows error-EventID:7036-LiveUpdate stopped P6: unspecified Common-Sys##Windows error-EventID:7036-LiveUpdate running P7: unspecified Common-Sys##Windows error-EventID:1054-The processing of Group Policy +failed. Windows could not obtain the name of a domain controller. Thi +s could be caused by a name resolution failure. Verify your Domain Na +me System (DNS) is configured and working correctly. P8:

Replies are listed 'Best First'.
Re: Combine two files into a single file
by JockoHelios (Scribe) on Jun 03, 2013 at 15:43 UTC
    I see the files you want to combine and the desired output, but I don't see the code you ran to try to produce the output you expect.

    Is this an omission in your post ?

    Dyslexics Untie !!!

      It's definitely an omission in the OP's post.... but perhaps s/he omitted reading On asking for help and How do I post a question effectively?.

      kar_thik82: If the gentle hints fail to move you, here's the compressed version: We're not code-a-matic. We're here to try to help you learn, but NOT to simply satisfy your 'gimme's.

      Then there's another: I know what I mean. Why don't you?. How do you determine which blank lines to retain and which to elide? The pattern for moving File1's content to output isn't the same, as far as I can see, as the pattern for moving 2's content.

      So, please consider UPDATING (with a note that you've done so) your OP to provide a tighter spec and the code you've tried along with explicit warnings and/or error message and a clear explanation of how the output fails to match your intent.


      If you didn't program your executable by toggling in binary, it wasn't really programming!