Professional Web Applications Themes

An extremely newbie question about appending records in a file. - PERL Beginners

Okay, so I do a "perl -pi -e 's/$/;/g' <filename>" to try and append a semicolon to the end of each record in a file in Linux. It does that just fine. Unfortunately it also prepends a semicolon onto the beginning of each record too. Now, I can vi the file and do a ":%s/$/;/g" and it works just fine. Why does Perl put a semicolon at the end AND beginning of each line? I have a feeling it involves the newline character but I''m not sure just how yet. I'm starting out with one-liners and working my self up ...

  1. #1

    Default An extremely newbie question about appending records in a file.

    Okay, so I do a "perl -pi -e 's/$/;/g' <filename>" to try and append a
    semicolon to the end of each record in a file in Linux.

    It does that just fine. Unfortunately it also prepends a semicolon onto
    the beginning of each record too.

    Now, I can vi the file and do a ":%s/$/;/g" and it works just fine.

    Why does Perl put a semicolon at the end AND beginning of each line? I
    have a feeling it involves the newline character but I''m not sure just
    how yet.

    I'm starting out with one-liners and working my self up to an actual
    script here. :>)

    Thanks,
    --Walt

    Walt Guest

  2. #2

    Default RE: An extremely newbie question about appending records in a file.

    Weaver, Walt wrote: 

    Well, not exactly. Note that there's no semicolon in front of the first
    line.

    You need to leave the "g" off. When you use /g, you're saying to repeat the
    substitution multiple times for each line. You only want one match for each
    line.

    What's happening is that perl lets $ match at the end of the line, or just
    before a trailing newline character. When you use /g, it ends up matching
    twice: once before the trailing newline and once at the very end of the
    string.

    So if the first line in the file is "foo", you wind up with:

    foo;\n <-- first replacement
    foo;\n; <-- second replacement

    The second semicolon will display at the front of the next line.

    Note that you can also achieve this by using the -l (ell) option. In this
    case perl strips the line terminator off before doing the substition and
    adds it back on before printing the line. Then /g can only find one match.

    But the problem is /g; just leave it off.
    Bob Guest

  3. #3

    Default Re: An extremely newbie question about appending records in a file.



    Weaver, Walt wrote:
     

    Untested but off hand somethign along the lines of:
    perl -pi -e 's/(.)$/\1\;/g' <filename>
     

    I bet one of the smarter folks can help you with the reasons why :)
     

    Cool! Perl rocks!
     

    HTH

    Lee.M - JupiterHost.Net
    Jupiterhost.Net Guest

  4. #4

    Default RE: An extremely newbie question about appending records in a file.

    On Wed, 2004-04-07 at 13:21, Bob Showalter wrote: 
    >
    > Well, not exactly. Note that there's no semicolon in front of the first
    > line.
    >
    > You need to leave the "g" off. When you use /g, you're saying to repeat the
    > substitution multiple times for each line. You only want one match for each
    > line.
    >
    > What's happening is that perl lets $ match at the end of the line, or just
    > before a trailing newline character. When you use /g, it ends up matching
    > twice: once before the trailing newline and once at the very end of the
    > string.
    >
    > So if the first line in the file is "foo", you wind up with:
    >
    > foo;\n <-- first replacement
    > foo;\n; <-- second replacement
    >
    > The second semicolon will display at the front of the next line.
    >
    > Note that you can also achieve this by using the -l (ell) option. In this
    > case perl strips the line terminator off before doing the substition and
    > adds it back on before printing the line. Then /g can only find one match.
    >
    > But the problem is /g; just leave it off.
    >[/ref]
    Thanks for the info. It's much appreciated!

    --Walt
    Walt Guest

Similar Threads

  1. newbie question: getting an embedded file
    By ludwigvb72@hotmail.com in forum Adobe Acrobat SDK
    Replies: 0
    Last Post: March 23rd, 01:21 AM
  2. Replies: 4
    Last Post: November 4th, 08:05 PM
  3. extremely simple newbie drawing ?
    By nanca webforumsuser@macromedia.com in forum Macromedia Flash Sitedesign
    Replies: 1
    Last Post: October 8th, 01:47 PM
  4. Newbie File Question
    By Janek Schleicher in forum PERL Miscellaneous
    Replies: 1
    Last Post: August 26th, 09:46 AM
  5. Replies: 1
    Last Post: July 18th, 07:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139