perl: adding lines and replacing stings

Discussion in 'Perl Misc' started by erobinson32, Mar 20, 2007.

  1. erobinson32

    erobinson32 Guest

    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?
     
    erobinson32, Mar 20, 2007
    #1
    1. Advertising

  2. erobinson32

    Paul Lalli Guest

    On Mar 20, 11:05 am, "erobinson32" <> 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
     
    Paul Lalli, Mar 20, 2007
    #2
    1. Advertising

  3. On Mar 20, 10:05 am, "erobinson32" <> 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..
     
    Kalyan Manchikanti, Mar 20, 2007
    #3
  4. erobinson32

    Paul Lalli Guest

    On Mar 20, 11:24 am, "Kalyan Manchikanti"
    <> wrote:
    > On Mar 20, 10:05 am, "erobinson32" <> 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
     
    Paul Lalli, Mar 20, 2007
    #4
  5. erobinson32

    erobinson32 Guest

    On Mar 20, 11:19 am, "Paul Lalli" <> wrote:
    > On Mar 20, 11:05 am, "erobinson32" <> 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!
     
    erobinson32, Mar 20, 2007
    #5
  6. erobinson32

    Ben Morrow Guest

    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.'
     
    Ben Morrow, Mar 20, 2007
    #6
  7. erobinson32

    Uri Guttman Guest

    >>>>> "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
     
    Uri Guttman, Mar 20, 2007
    #7
  8. erobinson32

    Paul Lalli Guest

    On Mar 20, 1:20 pm, Uri Guttman <> wrote:
    > >>>>> "PL" == Paul Lalli <> 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
     
    Paul Lalli, Mar 20, 2007
    #8
  9. erobinson32

    Paul Lalli Guest

    On Mar 20, 12:29 pm, Ben Morrow <> wrote:
    > 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" }'
    >


    Uhm, you're not, unfortunately. :p 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
     
    Paul Lalli, Mar 20, 2007
    #9
  10. erobinson32

    Ben Morrow Guest

    Quoth "Paul Lalli" <>:
    > On Mar 20, 12:29 pm, Ben Morrow <> wrote:
    > > 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.

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

    >
    > Uhm, you're not, unfortunately. :p 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
    # []
     
    Ben Morrow, Mar 20, 2007
    #10
  11. erobinson32

    Uri Guttman Guest

    >>>>> "PL" == Paul Lalli <> writes:

    PL> On Mar 20, 1:20 pm, Uri Guttman <> wrote:
    >> >>>>> "PL" == Paul Lalli <> 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.


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

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

    for those who do think and like/need speed and in many cases simplicity,
    it is more than just valid. like i said teaching line by line is good
    but not the only nor always the best way.

    and the determination of obscenely large is very vague. most text files
    that need processing are articles, code source, config files, etc. the
    large ones are notably logs and genetic data. so in most cases it is
    pretty easy to know if slurping will work fine. you general know the
    type of file you are processing unless you are making a truly general
    program like grep.

    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
     
    Uri Guttman, Mar 20, 2007
    #11
  12. Uri Guttman wrote:
    >>>>>>"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" ) ;



    perl -i -0777ne's/Nevada/California/g; print "FirstLine\n${_}LastLine\n"'




    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Mar 20, 2007
    #12
  13. Ben Morrow <> wrote:
    >
    > Quoth "Paul Lalli" <>:
    >> On Mar 20, 12:29 pm, Ben Morrow <> wrote:
    >> > 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.
    >> >
    >> > You're nearly there :)
    >> >
    >> > perl -pi -le'
    >> > BEGIN { print "FirstLine" }
    >> > s/California/Nevada/g;
    >> > END { print "LastLine" }'
    >> >

    >>
    >> Uhm, you're not, unfortunately. :p 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.



    But at least you took the heat that I would have gotten, as I was
    going to suggest the same thing. :)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 20, 2007
    #13
  14. erobinson32

    Paul Lalli Guest

    On Mar 20, 7:38 pm, Tad McClellan <> wrote:
    > Ben Morrow <> wrote:
    > > Yes, of course... and there I thought I was being so clever :(.
    > > Ach well.

    >
    > But at least you took the heat that I would have gotten, as I was
    > going to suggest the same thing. :)


    I did the same thing too - I just tested it before I posted. :p

    Paul Lalli
     
    Paul Lalli, Mar 21, 2007
    #14
  15. On 2007-03-20 18:20, Paul Lalli <> wrote:
    > 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.


    There is no method which is always valid.

    hp

    --
    _ | Peter J. Holzer | Blaming Perl for the inability of programmers
    |_|_) | Sysadmin WSR | to write clearly is like blaming English for
    | | | | the circumlocutions of bureaucrats.
    __/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
     
    Peter J. Holzer, Mar 23, 2007
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    478
  2. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    314
    Chris Hohmann
    Apr 11, 2005
  3. Phil Cooperking

    Searching Stings with Arrays?

    Phil Cooperking, Dec 20, 2006, in forum: Ruby
    Replies:
    11
    Views:
    182
    William James
    Dec 25, 2006
  4. joesplink

    Stings - textareas in Perl...

    joesplink, Mar 28, 2005, in forum: Perl Misc
    Replies:
    24
    Views:
    268
    Joe Smith
    Apr 3, 2005
  5. Markus Dehmann
    Replies:
    1
    Views:
    150
    Tad McClellan
    Sep 26, 2006
Loading...

Share This Page