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.

"Two Wheels good, Four wheels bad."

In reply to Have perl replace a bunch of text in lots of files by symgryph

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.