symgryph has asked for the wisdom of the Perl Monks concerning the following question:
I have a bunch of text files that need some 'post processing' performed on them. Additionally, I want to name the file according to a specific regex contained within each file itself. I have started programming, but can't get filehandles to be turned into an array (which I then use to replace text).
Here is an example of an input file
get system status: Version: FortiGate-60D v5.0,build4459,140410 (GA) Virus-DB: 23.00950(2015-03-02 19:54) Extended DB: 23.00950(2015-03-02 19:53) IPS-DB: 5.00616(2015-02-26 01:09) IPS-ETDB: 0.00000(2001-01-01 00:00) Serial-Number: FGT60D46234234234 Botnet DB: 1.00000(2012-05-28 22:51) BIOS version: 04000023 System Part-Number: P14482-03 Log hard disk: Available Internal Switch mode: interface Hostname: HOSTNAME Operation Mode: NAT Current virtual domain: root Max number of virtual domains: 10 Virtual domains status: 1 in NAT mode, 0 in TP mode Virtual domain configuration: disable FIPS-CC mode: disable Current HA mode: a-p, master Branch point: 271 Release Version Information: GA System time: Tue Mar 3 12:38:43 2015 -------------------------------------------------------------:
Here is what I hope it will output as:
HOSTNAME # get system status Version: FortiGate-60D v5.0,build4459,140410 (GA) Virus-DB: 23.00950(2015-03-02 19:54) Extended DB: 23.00950(2015-03-02 19:53) IPS-DB: 5.00616(2015-02-26 01:09) IPS-ETDB: 0.00000(2001-01-01 00:00) Serial-Number: FGT60D46234234234 Botnet DB: 1.00000(2012-05-28 22:51) BIOS version: 04000023 System Part-Number: P14482-03 Log hard disk: Available Internal Switch mode: interface Hostname: HOSTNAME Operation Mode: NAT Current virtual domain: root Max number of virtual domains: 10 Virtual domains status: 1 in NAT mode, 0 in TP mode Virtual domain configuration: disable FIPS-CC mode: disable Current HA mode: a-p, master Branch point: 271 Release Version Information: GA System time: Tue Mar 3 12:38:43 2015 #HOSTNAME -----------------------------------:
Finally I would like it to rename each file from its original name to the hostname matched by the 'hostname' match within the program.
#!/usr/bin/perl -w open DATA, "*.txt"; @file=<DATA>; while (@file) { if (/Hostname\:\s(\S+)/) { $hostname=$1; print "$hostname\n" } if (/\:$/) { print "# $hostname \n $_"; } }
I can't get the system to even print successfully I get loads of errors. I do know that th regexes are right, and have tested these independently. I also know that I can use a 'rename' function to rename the files, but am not sure how to get the variables to keep state between all the files.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Have perl replace a bunch of text in lots of files
by toolic (Bishop) on Mar 03, 2015 at 21:57 UTC | |
by cheako (Beadle) on Mar 04, 2015 at 20:23 UTC | |
by Anonymous Monk on Mar 04, 2015 at 20:45 UTC | |
|
Re: Have perl replace a bunch of text in lots of files
by cheako (Beadle) on Mar 04, 2015 at 20:21 UTC | |
|
Re: Have perl replace a bunch of text in lots of files
by cheako (Beadle) on Mar 05, 2015 at 00:28 UTC |