Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl: adding lines and replacing stings

Reply
Thread Tools

perl: adding lines and replacing stings

 
 
erobinson32
Guest
Posts: n/a
 
      03-20-2007

I would like to do three things in a single Perl script:
1. Add the text "FirstLine" to the very first line of a sample file.
2. Add the test "LastLine" to the very last line of a sample file.
3. Replace all of the instances of 'California' to 'Nevada' in a file.

I've just been running a few commands to accomplish this, but would
like to simplify the process:

perl -pi -e 's/California/Nevada/g' testfile
sed '1i\
FirstLine' testfile > temp_file
mv temp_file testfile
sed '$a\
LastLine' testfile > temp_file
mv temp_file testfile


Original File:
test1 - California
test2 - Oregon
test3 - Texas

Updated File:
FirstLine
test1 - Nevada
test2 - Oregon
test3 - Texas
LastLine

Is this possible? If not, is there a more automatic way of doing
this? updating my .bashrc?

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      03-20-2007
On Mar 20, 11:05 am, "erobinson32" <samdani...@gmail.com> wrote:
> I would like to do three things in a single Perl script:
> 1. Add the text "FirstLine" to the very first line of a sample file.
> 2. Add the test "LastLine" to the very last line of a sample file.
> 3. Replace all of the instances of 'California' to 'Nevada' in a file.
>
> I've just been running a few commands to accomplish this, but would
> like to simplify the process:
>
> perl -pi -e 's/California/Nevada/g' testfile
> sed '1i\
> FirstLine' testfile > temp_file
> mv temp_file testfile
> sed '$a\
> LastLine' testfile > temp_file
> mv temp_file testfile
>
> Original File:
> test1 - California
> test2 - Oregon
> test3 - Texas
>
> Updated File:
> FirstLine
> test1 - Nevada
> test2 - Oregon
> test3 - Texas
> LastLine
>
> Is this possible? If not, is there a more automatic way of doing
> this? updating my .bashrc?


This is a Perl "one-liner", though in this case, that's rather a
misnomer...

$ cat sample.txt
test1 - California
test2 - Oregon
test3 - Texas

$ perl -pi -e'
$_ = "First Line\n$_" if $. == 1;
s/California/Nevada/;
$_ .= "Last Line\n" if eof;
' sample.txt

$ cat sample.txt
First Line
test1 - Nevada
test2 - Oregon
test3 - Texas
Last Line

Read more about the various command line options in
perldoc perlrun
And see also:
perldoc perlvar (for $.)
perldoc -f eof

Hope this helps,
Paul Lalli

 
Reply With Quote
 
 
 
 
Kalyan Manchikanti
Guest
Posts: n/a
 
      03-20-2007
On Mar 20, 10:05 am, "erobinson32" <samdani...@gmail.com> wrote:
> I would like to do three things in a single Perl script:
> 1. Add the text "FirstLine" to the very first line of a sample file.
> 2. Add the test "LastLine" to the very last line of a sample file.
> 3. Replace all of the instances of 'California' to 'Nevada' in a file.
>
> I've just been running a few commands to accomplish this, but would
> like to simplify the process:
>
> perl -pi -e 's/California/Nevada/g' testfile
> sed '1i\
> FirstLine' testfile > temp_file
> mv temp_file testfile
> sed '$a\
> LastLine' testfile > temp_file
> mv temp_file testfile
>
> Original File:
> test1 - California
> test2 - Oregon
> test3 - Texas
>
> Updated File:
> FirstLine
> test1 - Nevada
> test2 - Oregon
> test3 - Texas
> LastLine
>
> Is this possible? If not, is there a more automatic way of doing
> this? updating my .bashrc?


perldoc -f unshift ( to add a first line to your existing file)..
perldoc -f push ( to add a last line to your existing file)..
perldoc -f tr, perldoc -f substr and ofcourse s/ ..are just three of
the many ways you can achieve string substitution..

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      03-20-2007
On Mar 20, 11:24 am, "Kalyan Manchikanti"
<kalyan.manchika...@gmail.com> wrote:
> On Mar 20, 10:05 am, "erobinson32" <samdani...@gmail.com> wrote:


> > 1. Add the text "FirstLine" to the very first line of a
> > sample file.
> > 2. Add the test "LastLine" to the very last line of a sample
> > file.


> perldoc -f unshift ( to add a first line to your existing file)..
> perldoc -f push ( to add a last line to your existing file)..


Gah. You are suggesting the OP read the entire file into memory,
modify the array, and then print the entire array back to the file?
Please don't do this. Ever.

Paul Lalli

 
Reply With Quote
 
erobinson32
Guest
Posts: n/a
 
      03-20-2007
On Mar 20, 11:19 am, "Paul Lalli" <mri...@gmail.com> wrote:
> On Mar 20, 11:05 am, "erobinson32" <samdani...@gmail.com> wrote:
>
>
>
> > I would like to do three things in a single Perl script:
> > 1. Add the text "FirstLine" to the very first line of a sample file.
> > 2. Add the test "LastLine" to the very last line of a sample file.
> > 3. Replace all of the instances of 'California' to 'Nevada' in a file.

>
> > I've just been running a few commands to accomplish this, but would
> > like to simplify the process:

>
> > perl -pi -e 's/California/Nevada/g' testfile
> > sed '1i\
> > FirstLine' testfile > temp_file
> > mv temp_file testfile
> > sed '$a\
> > LastLine' testfile > temp_file
> > mv temp_file testfile

>
> > Original File:
> > test1 - California
> > test2 - Oregon
> > test3 - Texas

>
> > Updated File:
> > FirstLine
> > test1 - Nevada
> > test2 - Oregon
> > test3 - Texas
> > LastLine

>
> > Is this possible? If not, is there a more automatic way of doing
> > this? updating my .bashrc?

>
> This is a Perl "one-liner", though in this case, that's rather a
> misnomer...
>
> $ cat sample.txt
> test1 - California
> test2 - Oregon
> test3 - Texas
>
> $ perl -pi -e'
> $_ = "First Line\n$_" if $. == 1;
> s/California/Nevada/;
> $_ .= "Last Line\n" if eof;
> ' sample.txt
>
> $ cat sample.txt
> First Line
> test1 - Nevada
> test2 - Oregon
> test3 - Texas
> Last Line
>
> Read more about the various command line options in
> perldoc perlrun
> And see also:
> perldoc perlvar (for $.)
> perldoc -f eof
>
> Hope this helps,
> Paul Lalli


Thanks Paul, that worked great!

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      03-20-2007

Quoth "erobinson32" <>:
>
> I would like to do three things in a single Perl script:
> 1. Add the text "FirstLine" to the very first line of a sample file.
> 2. Add the test "LastLine" to the very last line of a sample file.
> 3. Replace all of the instances of 'California' to 'Nevada' in a file.
>
> I've just been running a few commands to accomplish this, but would
> like to simplify the process:
>
> perl -pi -e 's/California/Nevada/g' testfile
> sed '1i\
> FirstLine' testfile > temp_file
> mv temp_file testfile
> sed '$a\
> LastLine' testfile > temp_file
> mv temp_file testfile


You're nearly there

perl -pi -le'
BEGIN { print "FirstLine" }
s/California/Nevada/g;
END { print "LastLine" }'

Ben

--
'Deserve [death]? I daresay he did. Many live that deserve death. And some die
that deserve life. Can you give it to them? Then do not be too eager to deal
out death in judgement. For even the very wise cannot see all ends.'

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      03-20-2007
>>>>> "PL" == Paul Lalli <> writes:

>> perldoc -f push ( to add a last line to your existing file)..


PL> Gah. You are suggesting the OP read the entire file into memory,
PL> modify the array, and then print the entire array back to the file?
PL> Please don't do this. Ever.

why not? if the file is small enough (and small is pretty big by today's
ram standards) it is simpler and faster to slurp in many cases. this
could be done with:

use File::Slurp ;

my $text = read_file( 'file' ) ;
$text =~ s/Nevada/California/g ;
write_file( 'file', "FirstLine\n", $text, "LastLine\n" ) ;

and when the edit_file feature is added it would be something like:

edit_file( 'file', sub{ s/Nevada/California/g ;
$_ = "FirstLine\n${_}LastLine\n" } ) ;

i totally get line by line processing but the bias against slurping
small files is silly IMO. with stdio or filesystem buffer sizes like 64k
now, you don't save any real ram with line by line and slurping usually
will be faster.

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      03-20-2007
On Mar 20, 1:20 pm, Uri Guttman <u...@stemsystems.com> wrote:
> >>>>> "PL" == Paul Lalli <mri...@gmail.com> writes:

>
> PL> Gah. You are suggesting the OP read the entire file into memory,
> PL> modify the array, and then print the entire array back to the file?
> PL> Please don't do this. Ever.
>
> why not? if the file is small enough (and small is pretty big by today's
> ram standards) it is simpler and faster to slurp in many cases.


Because far too many people program via the "copy-and-paste" method,
rather than the "think" method, and when they see a piece of code that
modifies a file by slurping it, they won't stop to think that the
method isn't as valid for their situation just because their file is
obscenely larger.

I just don't see the point of using a method that's only sometimes
valid as opposed to using one that's always valid.

Paul Lalli

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      03-20-2007
On Mar 20, 12:29 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth "erobinson32" <samdani...@gmail.com>:
>
>
>
>
>
>
>
> > I would like to do three things in a single Perl script:
> > 1. Add the text "FirstLine" to the very first line of a sample file.
> > 2. Add the test "LastLine" to the very last line of a sample file.
> > 3. Replace all of the instances of 'California' to 'Nevada' in a file.

>
> > I've just been running a few commands to accomplish this, but would
> > like to simplify the process:

>
> > perl -pi -e 's/California/Nevada/g' testfile
> > sed '1i\
> > FirstLine' testfile > temp_file
> > mv temp_file testfile
> > sed '$a\
> > LastLine' testfile > temp_file
> > mv temp_file testfile

>
> You're nearly there
>
> perl -pi -le'
> BEGIN { print "FirstLine" }
> s/California/Nevada/g;
> END { print "LastLine" }'
>


Uhm, you're not, unfortunately. Did you actually try this? The -
i feature takes affect only during the while(<>) {} loop created by -
p, and BEGIN{} and END{} blocks happen outside that loop. End result
- the two blocks print to STDOUT rather than the file.

Paul Lalli

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      03-20-2007

Quoth "Paul Lalli" <>:
> On Mar 20, 12:29 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> > Quoth "erobinson32" <samdani...@gmail.com>:
> >
> > > I would like to do three things in a single Perl script:
> > > 1. Add the text "FirstLine" to the very first line of a sample file.
> > > 2. Add the test "LastLine" to the very last line of a sample file.
> > > 3. Replace all of the instances of 'California' to 'Nevada' in a file.

> >
> > You're nearly there
> >
> > perl -pi -le'
> > BEGIN { print "FirstLine" }
> > s/California/Nevada/g;
> > END { print "LastLine" }'
> >

>
> Uhm, you're not, unfortunately. Did you actually try this?


Well, clearly not. Sorry about that .

> The -i feature takes affect only during the while(<>) {} loop created
> by -p, and BEGIN{} and END{} blocks happen outside that loop. End
> result - the two blocks print to STDOUT rather than the file.


Yes, of course... and there I thought I was being so clever . Ach
well.

Ben

--
#!/bin/sh
quine="echo 'eval \$quine' >> \$0; echo quined"
eval $quine
# []
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: They're starting up with the STINGS again zvnteq7 Computer Support 1 05-10-2009 12:36 AM
Re: They're starting up with the STINGS again Pennywise@DerryMaine.Gov Computer Support 0 05-09-2009 07:54 AM
Searching Stings with Arrays? Phil Cooperking Ruby 11 12-25-2006 10:06 PM
Replacing - and not Replacing... Rob Meade ASP General 5 04-11-2005 06:49 PM
Stings - textareas in Perl... joesplink Perl Misc 24 04-03-2005 10:07 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57