Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I am trying to open a text file and check if the first line has no header, it will be blank and replace this blank line with this line with are the header as you can see in the code posted. I know I am missing something cause nothing is happening.
I can not change the name of the original file. Any help would be very appreciated!
#!/usr/bin/perl use strict; use warnings; my $filename = 'dir/people.txt'; open(my $fh_out, '>', $filename) or die "Could not open file out '$fi +lename' $!"; open(my $fh_in, '<', $filename) or die "Could not open file in'$filen +ame' $!"; # Read in line at a time while( my $line = <$fh_in> ) { # If the file start with blank line replace it with these headers if ( $line =~ /^\S/ ) { print $fh_out, "Name, City, State, Zip"; } } close $fh_in; close $fh_out;
Text file:
my $file = " JOHND,NY,NY,11111 MARY,SOMEWHERE,OK,22222 JOE,LONDON,LO,33444 ";

I would like to add the header if the file doesn't have one:
Name,City,State,Zip JOHND,NY,NY,11111 MARY,SOMEWHERE,OK,22222 JOE,LONDON,LO,33444

Thanks for helping out!; open(my $fh_out,

Replies are listed 'Best First'.
Re: Replace first blank line in a text file with headers.
by toolic (Bishop) on Mar 24, 2016 at 15:00 UTC
    Here's a way to do it with Tie::File:
    use strict; use warnings; use Tie::File; my $filename = 'dir/people.txt'; tie my @array, 'Tie::File', $filename or die "Could not open file '$fi +lename' $!"; $array[0] = 'Name, City, State, Zip' unless $array[0] =~ /\S/; untie @array;
      Never used "Tie::File" before:
      "Using Tie::File module. This module makes a file look like a Perl arr +ay,each array element corresponds to a line of the file. Tie::File is very efficient; instead of rewriting the entire file, it +just rewrites what is necessary to apply the modification you specify +."
      Thanks for the idea!!!
      It works the first time, but the second time I run the code the blank line returns because its not on the first line any more,but its the first line on the new chunk of data been append to the file.
      Could I run this:
      tie my @array, 'Tie::File', $filename or die "Could not open file '$fi +lename' $!"; $array[0] = 'Name, City, State, Zip' unless $array[0] =~ /A\s*\Z/; $_ = '' unless $_ =~ /A\s*\Z/; untie @array;
      To get rid of any other empty line in the file once I check for the empty line been at the beginning of it? </code>
      Hi, I am using Tie::File, since it seems to be a straight forward way, could someone take a look if there is a better way of doing this?
      tie my @array, 'Tie::File', $filename or die "Could not open file '$fi +lename' $!"; # First if the first line does not start with "Name" it means there is +n't any headers, # add headers to the file: unshift @array, 'Name, City, State, Zip' unless $array[0] =~ /Name/; # Check the whole file if there is any blank lines, if there is, get r +id of it @array = grep /\S/, @array; untie @array;
      Thank you!
Re: Replace first blank line in a text file with headers.
by shadowsong (Pilgrim) on Mar 24, 2016 at 15:29 UTC

    If you find that the only place your text file could possibly have an empty line and you don't mind changing the file in place, feel free to use this one-liner (where filename.txt is the name of your text file):

    perl -i.tmp -lpe "s/^\s*$/Name,City,State,Zip/g" filename.txt

    Cheers,
    Shadowsong

      You can always change it ONLY on the first line, with linenumber $.

      perl -pi -le 's/^\s*$/Name, City, State, Zip/ if $.==1' filename.txt

      edit: Sorry, forgot the -l (without it, the line is replace-inserted without newline, and the next line with data does not start at the beginning of the next line)

Re: Replace first blank line in a text file with headers.
by neilwatson (Priest) on Mar 24, 2016 at 14:47 UTC

    Your if statement appears to match the beginning of a line followed by none white space (^\S). Did you mean ^\s* ? or the more accurate \A\s*\Z.

    Neil Watson
    watson-wilson.ca

Re: Replace first blank line in a text file with headers.
by ExReg (Priest) on Mar 24, 2016 at 15:34 UTC

    Assuming you make the changes suggested by neilwatson, another thing that might not go as expected is if you have blank lines further down in the file.

    while( my $line = <$fh_in> ) {

    will cause any blank line, not just the first one, to be replaced by "Name, City, State, Zip".

      Those changes won't work anyway as the code stands, because the OP code is opening the same file twice, once in read mode and once in write mode. That's a no-go.

      The Tie::File module is a solution, a one-liner using in-line option is another. Else, opening another file for writing, making the necessary changes, and renaming the files is another option.

      Then only should we worry about avoiding to change other lines, one way or another.

        I would have some code to make a backup of some kind, even if I used Tie::File. There is just so much that can go wrong when debugging a new version or some slightly different input file shows up and program fails. I've saved my own butt many times with a .bak1, .bak2 file...This stuff can go in a temp directory that periodically gets cleaned out. Creating long lived "junk" is also a problem, but of a different sort. If you have backups from yesterday that's a good thing, but I never want to resort to that to fix something that just went wrong 5 minutes ago.

        Too true.