in reply to While.For loop noob query
Also, i don't think you want to chomp the input and then not write a newline to the output file (so i just omitted the chomp from my snippet)...open(TXTIN,"ARGV[0]") || die "Cannot open the data file"; open(TXTOUT,">ARGV[1]") || die "Cannot open the formatted file"; while(<TXTIN>){ # gets a line from input file if( eof TXTIN ){ # eof returns true if the _next_ re +ad would fail, meaning s/different/regex here/; # the one we just read was the l +ast line }else{ s/yada/yadda/; } print TXTOUT $_; } close(TXTIN); close(TXTOUT);
And if you want to do an in-place edit (see perlrun) you can just add the -i command-line parameter.perl -pe 'if( !eof ){ s/^1/AAAA/; } else{ s/^1/BBBB/; }' /etc/hosts > +/tmp/newfile
|
|---|