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

Hi fellow monastery dwellers
I have a problem that should be easy to solve, but eludes me. I have a bunch of files of the form
test
test 1
test 2

and I want to modify these to
test 1.0
test 1
test 2

I am using the following code
#!/usr/bin/perl -w # the goal of this script is to change test in line 1 of files to test + 1.0 my $txtfile; #while (<*.TXT>){ foreach $txtfile (glob("*.TXT")) { print $txtfile; print "\n"; open FILE, "$txtfile"; open NEWFILE, ">$txtfile.tmp"; while (<FILE>){  if ($. == 1){   $_ =~ s/$_/$_ 1.0/g;   }  print NEWFILE $_; } close FILE; close NEWFILE; }
However this gives me something of the form
test
   1.0test 1
test 2

The first line is not always test, but I do want to retain whatever is there and just add a space and 1.0. I know womewhere I should be chomping of a newline and adding one to the 1.0 .. or at least that is what the output indicates. Does anyone have any pointers

Thanks

Deepak

"What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'

Replies are listed 'Best First'.
Re: appending to a particular line in a file
by dvergin (Monsignor) on Aug 03, 2001 at 02:30 UTC
    When you read in your first line, it has the form      1\n with a trailing newline. So when you say:      $_ =~ s/$_/$_ 1.0/g; you are saying, "If $_ can be found in the variable $_ (which of course it can), substitute for the entire value, that same value with " 1.0" on the end. Which results in the string:      1\n 1.0 Which is just what you report.

    Doing a substitution on the whole string is a bit odd anyway. And you don't need the /g. How about (staying with the level of verbose clarity you are using):

    foreach $txtfile (glob("*.TXT")) { print $txtfile; print "\n"; open FILE, "$txtfile"; open NEWFILE, ">$txtfile.tmp"; while (<FILE>){ chomp $_; if ($. == 1){ $_ .= " 1.0"; # i.e. $_ = $_ . " 1.0"; } print NEWFILE $_, "\n"; } close FILE; close NEWFILE; }
    Update: A more streamlined version might be:
    foreach $txtfile (glob("*.TXT")) { print $txtfile; print "\n"; open FILE, "$txtfile"; open NEWFILE, ">$txtfile.tmp"; while (<FILE>){ $_ =~ s/\n/ 1.0\n/ if $. = 1; print NEWFILE $_; } close FILE; close NEWFILE; }
      Thank you for your reply. I have it working now with the following little change
      if ($. == 1){   chomp($_); #  $_ =~ s/$_/$_ 1.0/g;   $_ .= " 1.0\n"; # i.e. $_ = $_ . " 1.0"; }

      Cheers
      mndoci

      "What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'
Re: appending to a particular line in a file
by mndoci (Scribe) on Aug 03, 2001 at 02:00 UTC
    Just a note .. I want to do this without resorting to command line approaches, i.e. as part of a much larger program

    "What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'