hmag has asked for the wisdom of the Perl Monks concerning the following question:
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:#!/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.
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#!/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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Splitting up file
by linux454 (Pilgrim) on Mar 19, 2006 at 20:48 UTC | |
|
Re: Splitting up file
by matze (Friar) on Mar 19, 2006 at 20:55 UTC | |
|
Re: Splitting up file
by graff (Chancellor) on Mar 19, 2006 at 22:29 UTC | |
|
Re: Splitting up file
by hmag (Acolyte) on Mar 20, 2006 at 05:15 UTC | |
|
Re: Splitting up file
by Anonymous Monk on Mar 20, 2006 at 05:03 UTC | |
|
Re: Splitting up file
by hmag (Acolyte) on Mar 20, 2006 at 08:53 UTC |