Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: File Find/Replace with the replacement coming from part of earlier matched string

by Kenosis (Priest)
on Feb 05, 2013 at 13:51 UTC ( #1017166=note: print w/ replies, xml ) Need Help??


in reply to File Find/Replace with the replacement coming from part of earlier matched string

One option is to create a directory in your log files' directory for the prepped log files, so the originals aren't altered. Given that the log files' format is as shown, consider the following which writes prepped files into a directory called prepped:

use strict; use warnings; my $pcName; local $/ = ''; # paragraph mode for my $file (<*.log>) { open my $fhIN, '<', $file or die $!; open my $fhOUT, '>', 'prepped/' . $file or die $!; while (<$fhIN>) { chomp; print $fhOUT $_ if /\n/; # first two file lines next if /^(PCName:\s+.+)/ and $pcName = $1; # pc name & next print $fhOUT "\n\n$pcName\n$_"; # print pc name & command } close $fhIN; close $fhOUT; }

Sample file output from your data:

PCName: Foo1 Command1:dfie PCName: Foo1 Command2:dfo PCName: Foo1 Command3:dfum PCName: Foo2 Command1:dfie PCName: Foo2 Command2:dfo PCName: Foo2 Command3:dfum

The fileglob operator <*.log> is used to get the list of files in the log directory; you may need to change the file extension.

Hope this helps!


Comment on Re: File Find/Replace with the replacement coming from part of earlier matched string
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017166]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-07-29 08:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (212 votes), past polls