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'
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.