On Wed, 2004-04-07 at 13:21, Bob Showalter wrote:
> Weaver, Walt wrote:
> > 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.
> 
> 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.
> 
Thanks for the info. It's much appreciated!

--Walt

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to