Hi all, I have a problem - I have a script which I need to split the following file into 3 sections:
#!/bin/sh . /etc/rc.common # __START_CONFIG__ binary_app=/usr/bin/true config_file=/etc/inetd.conf MOD_NAME="ABC" # __END_CONFIG__ # this file does nothing.
There are 3 things I need to do here: 1 - I need to grab the all parts up to & including # __START_CONFIG__ & store them in a temp file 123456.beg 2 - I need to grab the next section up to the line with # __END_CONFIG__ & put it into an array called allow_edit 3 - I need to grab the end of the file & store it into a temp file 123456.end Here's what I have so far:
#!/usr/bin/perl # Open File - Read File Contents Then Modify & save contents # Specify name of file $data_file="sample.pm"; # Name of temp file 1 $prefile1="/tmp/123456.beg" # Name of temp file 2 $prefile2="/tmp/123456.end" $action=1; # Open File abd read it all in to rawdata open (outfile1, ">$prefile1") || die ("Could not open file. <br> $!"); +# Open The File open (outfile2, ">$prefile2") || die ("Could not open file. <br> $!"); +# Open The File open (sample, "$data_file") || die ("Could not open file. <br> $!");# +Open The File flock(sample, 2) or die "cannot lock file exclusively: $!";# Lock The +File @rawdata = <sample>;# Put data from file into array called sample # write data from sample.pm into beg_non_edit foreach $value (@rawdata) { print ("$value\n"); if($string =~ /# __START_CONFIG__/i) { $action=2; } if($string =~ /# __END_CONFIG__/i) { $action=3; } if ( $action == 1 ) { # write to outfile1 print outfile1 "$value"; } if ($action == 3) { # write to outfile2 print outfile2 "$value"; } if ($action == 2) { # copy string to new array } } close (sample); close (outfile1); close (outfile2);
My problem is that I can't seem to write into the new files the information - they appear blank - obviously my code is wrong. Secondly, If I then try to rerun the script, it tells me that I cannot open the files. Any help would be really appreciated - thanks, Terry

In reply to Splitting up file by hmag

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.