Script to prepend text to beginning of file
I need to add 2 lines to a file and add the following text.
ENTHDR|1|3.0
STAGEHDR|Barcoded
I don't have any idea how to do this in Perl
Thanks
Steve
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Script to prepend text to beginning of file
--90e6ba4767bf47279e049a5bb075
Content-Type: text/plain; charset=ISO-8859-1
A simple way:
#!/usr/bin/perl -w
open FILE, ">>file.txt";
print FILE "line1\n";
print FILE "line2\n";
print FILE "ENTHDR|1|3.0\n";
print FILE "STAGEHDR|Barcoded\n";
close FILE;
On Thu, Jan 20, 2011 at 3:57 PM, steve1040 <steveandtrina [at] gmail.com> wrote:
> I need to add 2 lines to a file and add the following text.
>
> ENTHDR|1|3.0
> STAGEHDR|Barcoded
>
> I don't have any idea how to do this in Perl
>
> Thanks
> Steve
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>
--90e6ba4767bf47279e049a5bb075--
Re: Script to prepend text to beginning of file
Hi Steve,
On Thursday 20 Jan 2011 20:57:50 steve1040 wrote:
> I need to add 2 lines to a file and add the following text.
>
> ENTHDR|1|3.0
> STAGEHDR|Barcoded
>
> I don't have any idea how to do this in Perl
>
In UNIX and UNIX-like systems (including Windows), it is useful to think of a
file as a big sequence of octets, where you can overwrite or append data if
it's the same width, but you cannot insert stuff in the middle (or the
beginning) or remove stuff from inside easily.
The best way to prepend some data to the beginning is to write a new file and
then to move it on top of the existing file: (tested:)
[code]
#!/usr/bin/perl
use strict;
use warnings;
my $filename = shift( [at] ARGV);
my $new_fn = $filename . '.new';
open my $in, '<', $filename
or die "Cannot open $filename for reading - $!";
open my $out, '>', $new_fn
or die "Cannot open $new_fn for writing - $!";
print {$out} <<'EOF';
ENTHDR|1|3.0
STAGEHDR|Barcoded
EOF
while (my $line = <$in>)
{
print {$out} $line;
}
close($out);
close($in);
rename($new_fn, $filename);
[/code]
Note that you can also use file I/O abstractions such as Tie::File
( http://perldoc.perl.org/Tie/File.html ) or IO::All (
http://search.cpan.org/dist/IO-All/ ) to write it more succinctly.
Regards,
Shlomi Fish
--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Why I Love Perl - http://shlom.in/joy-of-perl
Chuck Norris can make the statement "This statement is false" a true one.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Script to prepend text to beginning of file
Hi Ary,
On Friday 21 Jan 2011 16:01:36 Ary Kleinerman wrote:
> A simple way:
>
> #!/usr/bin/perl -w
> open FILE, ">>file.txt";
> print FILE "line1\n";
> print FILE "line2\n";
> print FILE "ENTHDR|1|3.0\n";
> print FILE "STAGEHDR|Barcoded\n";
> close FILE;
>
This script will *append* 4 lines to "file.txt" - not prepend them and
furthermore does not use:
1. Three args open.
2. Lexical filehandles.
3. or die after an open.
4. "use strict;" and "use warnings;".
5. Still uses the "-w" flag.
As a result it suffers from many bad practices. See:
http://perl-begin.org/tutorials/bad-elements/
=46rom where did you learn Perl that you recommend writing such crude code?
Regards,
Shlomi Fish
> On Thu, Jan 20, 2011 at 3:57 PM, steve1040 <steveandtrina [at] gmail.com> wrot=
e:
> > I need to add 2 lines to a file and add the following text.
> >
> > ENTHDR|1|3.0
> > STAGEHDR|Barcoded
> >
> > I don't have any idea how to do this in Perl
> >
> > Thanks
> > Steve
> >
> >
> > --
> > To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> > For additional commands, e-mail: beginners-help [at] perl.org
> > http://learn.perl.org/
=2D-
=2D--------------------------------------------------------- -------
Shlomi Fish http://www.shlomifish.org/
Best Introductory Programming Language - http://shlom.in/intro-lang
Chuck Norris can make the statement "This statement is false" a true one.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Script to prepend text to beginning of file
--90e6ba6e819c6f1ed4049a5d0f5c
Content-Type: text/plain; charset=ISO-8859-1
Shlomi,
On Fri, Jan 21, 2011 at 12:13 PM, Shlomi Fish <shlomif [at] iglu.org.il> wrote:
> Hi Ary,
>
> On Friday 21 Jan 2011 16:01:36 Ary Kleinerman wrote:
> > A simple way:
> >
> > #!/usr/bin/perl -w
> > open FILE, ">>file.txt";
> > print FILE "line1\n";
> > print FILE "line2\n";
> > print FILE "ENTHDR|1|3.0\n";
> > print FILE "STAGEHDR|Barcoded\n";
> > close FILE;
> >
>
> This script will *append* 4 lines to "file.txt" - not prepend them and
> furthermore does not use:
>
> 1. Three args open.
>
> 2. Lexical filehandles.
>
> 3. or die after an open.
>
> 4. "use strict;" and "use warnings;".
>
> 5. Still uses the "-w" flag.
>
>
The steve's mail, in this thread, said the following:
"I need to add 2 lines to a file and add the following text.
ENTHDR|1|3.0
STAGEHDR|Barcoded"
So... my code do exactly that.
As a result it suffers from many bad practices. See:
>
> http://perl-begin.org/tutorials/bad-elements/
>
> I only showed the idea of the code. It's assumed that echo one should
improve it. This is the idea. But thats for this URL. I didn't know it and
It's very useful.
> From where did you learn Perl that you recommend writing such crude code?
>
>
From the street :P
> Regards,
>
> Shlomi Fish
>
> > On Thu, Jan 20, 2011 at 3:57 PM, steve1040 <steveandtrina [at] gmail.com>
> wrote:
> > > I need to add 2 lines to a file and add the following text.
> > >
> > > ENTHDR|1|3.0
> > > STAGEHDR|Barcoded
> > >
> > > I don't have any idea how to do this in Perl
> > >
> > > Thanks
> > > Steve
> > >
> > >
> > > --
> > > To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> > > For additional commands, e-mail: beginners-help [at] perl.org
> > > http://learn.perl.org/
>
> --
> ------------------------------------------------------------ -----
> Shlomi Fish http://www.shlomifish.org/
> Best Introductory Programming Language - http://shlom.in/intro-lang
>
> Chuck Norris can make the statement "This statement is false" a true one.
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply .
>
--90e6ba6e819c6f1ed4049a5d0f5c--
Re: Script to prepend text to beginning of file
Hi Ary,
On Friday 21 Jan 2011 17:39:48 Ary Kleinerman wrote:
> Shlomi,
>
> On Fri, Jan 21, 2011 at 12:13 PM, Shlomi Fish <shlomif [at] iglu.org.il> wrote:
> > Hi Ary,
> >
> > On Friday 21 Jan 2011 16:01:36 Ary Kleinerman wrote:
> > > A simple way:
> > >
> > > #!/usr/bin/perl -w
> > > open FILE, ">>file.txt";
> > > print FILE "line1\n";
> > > print FILE "line2\n";
> > > print FILE "ENTHDR|1|3.0\n";
> > > print FILE "STAGEHDR|Barcoded\n";
> > > close FILE;
> >
> > This script will *append* 4 lines to "file.txt" - not prepend them and
> > furthermore does not use:
> >
> > 1. Three args open.
> >
> > 2. Lexical filehandles.
> >
> > 3. or die after an open.
> >
> > 4. "use strict;" and "use warnings;".
> >
> > 5. Still uses the "-w" flag.
>
> The steve's mail, in this thread, said the following:
>
> "I need to add 2 lines to a file and add the following text.
>
> ENTHDR|1|3.0
> STAGEHDR|Barcoded"
>
> So... my code do exactly that.
>
Yes, but the subject line reads "Script to prepend text to beginning of file",
so you have not noticed that.
> > As a result it suffers from many bad practices. See:
> > http://perl-begin.org/tutorials/bad-elements/
> >
> I only showed the idea of the code. It's assumed that echo one should
>
> improve it. This is the idea. But thats for this URL. I didn't know it and
> It's very useful.
You should not write such sub-optimal code especially not as response to
beginners' questions on a beginners mailing list where they can learn bad
practices from it.
Regards,
Shlomi Fish
--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Apple Inc. is Evil - http://www.shlomifish.org/open-source/anti/apple/
Chuck Norris can make the statement "This statement is false" a true one.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Script to prepend text to beginning of file
--20cf304345481e0988049a617672
Content-Type: text/plain; charset=ISO-8859-1
>
> use strict;
> use warnings;
> use Tie::File;
>
> my [at] array;
> tie [at] array, 'Tie::File', FILENAME or die "Couldn't open file: $!"
>
> unshift [at] array, <<'END_PREPEND';
> ENTHDR|1|3.0
> STAGEHDR|Barcoded
> END_PREPEND
>
Haven't tested it, but it should work.
http://perldoc.perl.org/Tie/File.html
Brian.
On Thu, Jan 20, 2011 at 3:57 PM, steve1040 <steveandtrina [at] gmail.com> wrote:
> I need to add 2 lines to a file and add the following text.
>
> ENTHDR|1|3.0
> STAGEHDR|Barcoded
>
> I don't have any idea how to do this in Perl
>
> Thanks
> Steve
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>
--20cf304345481e0988049a617672--