Ask a Question related to PERL Beginners, Design and Development.
-
Chris Charley #1
Add text to beginning every line
Hi
Hope someone can help with this. I used to be able to do it, but now have
forgotten. I want to prepend some text to every line in a file. The program
below:
#!/usr/bin/perl
# prepend.pl - adds line number to beginning of every line in a file
use strict;
use warnings;
while (<>) {
s/^/Line: $. /;
}
Then, on the command line I type: perl prepend.pl somefile.txt, but
somefile.txt does not have the changes(Line: <linenumber>).
Thanks for any explanation you may offer.
Chris
Chris Charley Guest
-
vi, adding words to the beginning of each line
in vi-ing a file, i have a list of lines and they have different alphabets when they begin a new line. now i wanted to add this "more" to the... -
text line spacing and line tool
1.) Using InDesign 2.0.2 - When aligning text (in one particular text frame)to a base grid, the text will always skip one line even though the base... -
What does a semicolon do at the beginning of a line?
Was browsing the documentation on reading a configuration file and found this. What does a semicolon do at the beginning of a line? ; <?php DO... -
Text.line
Hi, I need to do a data transfer from an file .txt to cast member, but I need between the transfer, be make a division of text and in this before... -
Write line at beginning of file?
Eric Pement <pemente@northpark.edu> wrote: What version of perl are you referring to? -
Chris Charley #2
Re: Add text to beginning every line
"Chris Charley" <charleyNOSPAM@pulsenetNOSPAM.com> wrote in message news:...
[snip]Should be (works correctly):> while (<>) {
> s/^/Line: $. /;
> }
while (<>) {
s/^/Line: $. /;
print;
}
Then the command could be: perl prepend.pl somefile.txt > somefile.new
which would correctly print to the 'new' file the somefile.txt file with the
line numbers prepended.
Chris
Chris Charley Guest
-
Tim Johnson #3
RE: Add text to beginning every line
You're reading in the file, but not changing it. You're only changing it in
memory. Check out the responses to the other post called "make changes to a
file, then reading in those changes"
-----Original Message-----
From: Chris Charley [mailto:charleyNOSPAM@pulsenetNOSPAM.com]
Sent: Monday, October 06, 2003 11:20 AM
To: [email]beginners@perl.org[/email]
Subject: Add text to beginning every line
Hi
Hope someone can help with this. I used to be able to do it, but now have
forgotten. I want to prepend some text to every line in a file. The program
below:
#!/usr/bin/perl
# prepend.pl - adds line number to beginning of every line in a file use
strict; use warnings;
while (<>) {
s/^/Line: $. /;
}
Then, on the command line I type: perl prepend.pl somefile.txt, but
somefile.txt does not have the changes(Line: <linenumber>).
Thanks for any explanation you may offer.
Chris
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
Tim Johnson Guest
-
Thomas bätzler #4
RE: Add text to beginning every line
Hi,
Chris Charley <charleyNOSPAM@pulsenetNOSPAM.com> wrote:I would even suggest> Should be (works correctly):
>
> while (<>) {
> s/^/Line: $. /;
> print;
> }
>
> Then the command could be: perl prepend.pl somefile.txt >
> somefile.new
> which would correctly print to the 'new' file the
> somefile.txt file with the line numbers prepended.
perl -i.bak -pe 's/^/Line $.: /' <filenames>
This will do the replacement on one or more files
whose names oyu give on the command line. Perl
will then create backup files with the .bak extension
and modify the original files.
HTH,
Thomas
Thomas bätzler Guest
-
Sudarshan Raghavan #5
Re: Add text to beginning every line
Chris Charley wrote:
You don't need a regular expression to do this. This should suffice> "Chris Charley" <charleyNOSPAM@pulsenetNOSPAM.com> wrote in message news:...
> [snip]>> > while (<>) {
> > s/^/Line: $. /;
> > }
> Should be (works correctly):
>
> while (<>) {
> s/^/Line: $. /;
$_ = "Line: $. $_";
I hope you are aware of the fact that $. does not loop back to 0 when you pass
multiple files in the argument list.
When the call is 'perl prepend.pl somefile1.txt somefile2.txt', $. will not
loopback to 0 when the processing of 'somefile1.txt' finishes and
'somefile2.txt' starts.
perldoc perlop #Search for 'null filehandle'
>
> print;
> }
>
> Then the command could be: perl prepend.pl somefile.txt > somefile.new
> which would correctly print to the 'new' file the somefile.txt file with the
> line numbers prepended.
>
> Chris
>
> --
> To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> For additional commands, e-mail: [email]beginners-help@perl.org[/email]Sudarshan Raghavan Guest
-
Tore Aursand #6
Re: Add text to beginning every line
On Mon, 06 Oct 2003 14:20:05 -0400, Chris Charley wrote:
Try writing 'perl prepend.pl somefile.txt > newfile.txt' instead.> Then, on the command line I type: perl prepend.pl somefile.txt, but
> somefile.txt does not have the changes(Line: <linenumber>).
--
Tore Aursand <tore@aursand.no>
Tore Aursand Guest



Reply With Quote

