inserting lines

Discussion in 'Perl Misc' started by PG, Sep 20, 2006.

  1. PG

    PG Guest

    I want to know how to add a line , say "foobar" , after a line which says,
    for example "string1", in a inout file.

    how can I do this ?

    (I'm pretty new to Perl)
    PG, Sep 20, 2006
    #1
    1. Advertising

  2. PG

    David Squire Guest

    PG wrote:
    > I want to know how to add a line , say "foobar" , after a line which says,
    > for example "string1", in a inout file.
    >
    > how can I do this ?


    What have you tried so far?

    What is an "inout" file?


    DS
    David Squire, Sep 20, 2006
    #2
    1. Advertising

  3. PG

    Guest

    PG ha escrito:

    > I want to know how to add a line , say "foobar" , after a line which says,
    > for example "string1", in a inout file.
    >
    > how can I do this ?
    >
    > (I'm pretty new to Perl)


    perl -pe 'if (/string1/) { chomp; s/$/foobar\n/ }'

    The above, of course, is the same as the following script:

    #!/usr/bin/perl

    while (<>) {
    if (/string1/) { chomp; s/$/foobar\n/ };
    print
    }
    , Sep 20, 2006
    #3
  4. PG

    David Squire Guest

    wrote:
    > PG ha escrito:
    >
    >> I want to know how to add a line , say "foobar" , after a line which says,
    >> for example "string1", in a inout file.
    >>
    >> how can I do this ?
    >>
    >> (I'm pretty new to Perl)

    >
    > perl -pe 'if (/string1/) { chomp; s/$/foobar\n/ }'
    >


    .... but this does not solve the OP's problem, which does not mention
    substitution, but rather an additional line.

    Still waiting to here what the OP has tried...


    DS
    David Squire, Sep 20, 2006
    #4
  5. PG

    Uri Guttman Guest

    >>>>> "JG" == Jim Gibson <> writes:

    JG> The old-fashioned way is to:

    JG> 1. Open the old file for reading (perldoc -f open).
    JG> 2. Open a new file for writing.
    JG> 3. Read the old file a line-at-a-time, looking for "string1" and
    JG> writing out each line to the new file (perldoc -f print).
    JG> 4. If "string1" is found, write out the line "foobar" (after you copy
    JG> the string1 line to the new file.
    JG> 5. When the old file has been completely read and copied, close both
    JG> files.
    JG> 6. Either within your program or via the operating system after the
    JG> program has quit, rename the old file to the new file, after optionally
    JG> renaming the old file as a backup (perldoc -f rename).

    the new fashioned way to do this is with file::slurp and 1 s///g
    call. in the near future (i keep promising this) there will be edit_file()
    and edit_file_lines() subs added to that module that will do this in one
    call.

    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, Sep 20, 2006
    #5
  6. PG

    Guest

    David Squire <> wrote:
    > wrote:
    > > PG ha escrito:
    > >
    > >> I want to know how to add a line , say "foobar" , after a line which
    > >> says, for example "string1", in a inout file.
    > >>
    > >> how can I do this ?
    > >>
    > >> (I'm pretty new to Perl)

    > >
    > > perl -pe 'if (/string1/) { chomp; s/$/foobar\n/ }'
    > >

    >
    > ... but this does not solve the OP's problem, which does not mention
    > substitution, but rather an additional line.


    How is substituting an empty string with a line not the same
    thing as inserting a line? (although he is missing a newline, he
    should take out the chomp and move \n to the beginning of the
    replacement.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Sep 20, 2006
    #6
  7. PG

    David Squire Guest

    wrote:
    > David Squire <> wrote:
    >> wrote:
    >>> PG ha escrito:
    >>>
    >>>> I want to know how to add a line , say "foobar" , after a line which
    >>>> says, for example "string1", in a inout file.
    >>>>
    >>>> how can I do this ?
    >>>>
    >>>> (I'm pretty new to Perl)
    >>> perl -pe 'if (/string1/) { chomp; s/$/foobar\n/ }'
    >>>

    >> ... but this does not solve the OP's problem, which does not mention
    >> substitution, but rather an additional line.

    >
    > How is substituting an empty string with a line not the same
    > thing as inserting a line? (although he is missing a newline, he
    > should take out the chomp and move \n to the beginning of the
    > replacement.
    >


    Ah. I misread that. Sorry.


    DS
    David Squire, Sep 20, 2006
    #7
  8. PG wrote:
    > I want to know how to add a line , say "foobar" , after a line which says,
    > for example "string1", in a inout file.
    >
    > how can I do this ?


    perl -i -pe'$_ .= "foobar\n" if /string1/' inout_file



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Sep 20, 2006
    #8
  9. PG

    Ted Zlatanov Guest

    On 20 Sep 2006, wrote:

    > I want to know how to add a line , say "foobar" , after a line which says,
    > for example "string1", in a inout file.


    This will print the new data:

    perl -pe'$_ = "${_}foobar\n" if /string1/;' FILE

    This will rewrite FILE with the new data:

    perl -pie'$_ = "${_}foobar\n" if /string1/;' FILE

    Ted
    Ted Zlatanov, Sep 20, 2006
    #9
  10. Ted Zlatanov wrote:
    > On 20 Sep 2006, wrote:
    >
    >>I want to know how to add a line , say "foobar" , after a line which says,
    >>for example "string1", in a inout file.

    >
    > This will print the new data:
    >
    > perl -pe'$_ = "${_}foobar\n" if /string1/;' FILE
    >
    > This will rewrite FILE with the new data:
    >
    > perl -pie'$_ = "${_}foobar\n" if /string1/;' FILE


    That won't work as the -i switch requires an argument which is appended to the
    back-up file name:

    $ perl -pie'$_ = "${_}foobar\n" if /string1/;' FILE
    Unrecognized switch: -= "${_}foobar\n" if /string1/; (-h will show valid
    options).


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Sep 20, 2006
    #10
  11. PG

    Ted Zlatanov Guest

    On 20 Sep 2006, wrote:

    Ted Zlatanov wrote: > On 20 Sep 2006, wrote: >
    >>> I want to know how to add a line , say "foobar" , after a line which says,
    >>> for example "string1", in a inout file.

    >>
    >> This will print the new data:
    >>
    >> perl -pe'$_ = "${_}foobar\n" if /string1/;' FILE
    >>
    >> This will rewrite FILE with the new data:
    >>
    >> perl -pie'$_ = "${_}foobar\n" if /string1/;' FILE

    >
    > That won't work as the -i switch requires an argument which is appended to the
    > back-up file name:
    >
    > $ perl -pie'$_ = "${_}foobar\n" if /string1/;' FILE
    > Unrecognized switch: -= "${_}foobar\n" if /string1/; (-h will show valid
    > options).


    Yes. -pi -e will work. I was mesmerized by the food reference.

    Ted
    Ted Zlatanov, Sep 20, 2006
    #11
  12. PG wrote:
    > I want to know how to add a line , say "foobar" , after a line which
    > says, for example "string1", in a inout file.
    >
    > how can I do this ?


    Which part do you have problems with? Have you checked the FAQ 'perldoc -q
    insert':
    "How do I change one line in a file/delete a line in a file/insert a line in
    the middle of a file/append to the beginning of a file?"

    jue
    Jürgen Exner, Sep 21, 2006
    #12
  13. PG

    -berlin.de Guest

    Jim Gibson <> wrote in comp.lang.perl.misc:
    > In article <L7dQg.38696$>, PG
    > <> wrote:
    >
    > > I want to know how to add a line , say "foobar" , after a line which says,
    > > for example "string1", in a inout file.
    > >
    > > how can I do this ?
    > >
    > > (I'm pretty new to Perl)

    >
    > This is a "Frequently Asked Question". Unfortunately, the FAQ for Perl
    > only has one recommendation: using the Tie::File module. If you are new
    > to Perl, learning about the "tie" concept may confuse you, as may using
    > modules, although you will definitely benefit from learning how to use
    > modules.


    I too find it unfortunate that the FAQ doesn't explain the process
    in more detail. Typical file-manipulation tools (most notably editors)
    give the user the impression that a file is something like an array
    of lines on disk. Many beginning programmers have this mental image.
    Understanding that a file is really one homogeneous lump of bytes
    is a necessary step for a new programmer. This FAQ would be a good
    opportunity to set the concept right and explain the consequences.
    Instead it does all it can to preserve the inappropriate mental
    image.

    I think the original content of the FAQ (which explained the manual
    process of inserting lines etc.) should be put back in, rewritten if
    necessary. A good opportunity to win wealth and fame with a doc
    change :)

    Anno
    -berlin.de, Sep 21, 2006
    #13
  14. PG

    David Oswald Guest

    -berlin.de wrote:

    > Jim Gibson <> wrote in comp.lang.perl.misc:
    >
    > I too find it unfortunate that the FAQ doesn't explain the process
    > in more detail.


    As they say, "patches are welcome."
    David Oswald, Oct 3, 2006
    #14
    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. Jack
    Replies:
    9
    Views:
    2,638
  2. Roy Schestowitz

    Inserting Lines into HTML Header

    Roy Schestowitz, Jun 12, 2005, in forum: HTML
    Replies:
    10
    Views:
    986
    Andy Dingley
    Jun 13, 2005
  3. Joe Wright
    Replies:
    0
    Views:
    500
    Joe Wright
    Jul 27, 2003
  4. Replies:
    7
    Views:
    168
    Ben Morrow
    Jun 7, 2006
  5. Replies:
    14
    Views:
    391
    Stefan Ram
    Apr 11, 2013
Loading...

Share This Page