[Pdx-pm] filewise (not linewise) in-place editing?

Ben Prew ben.prew at gmail.com
Thu Oct 21 10:10:52 CDT 2004


On Thu, 21 Oct 2004 01:20:59 -0700, Ken Brush <ken at cgi101.com> wrote:
> On Wednesday 20 October 2004 16:22, Randall Hansen wrote:
> > folks ~
> >
> > i've just needed to munge a group of files to change and concatenate
> > data on several lines into one line.  usually i do something like:
> >
> > perl -pi -e 's/foo/bar/g' file1 ...
> >
> > this wouldn't work, since -p reads input files linewise.
> >
> > i wrote a little script to solve my problem, it resets $/ and sucks the
> > whole file into a variable, munges it, and writes it.  is there a way
> > to do this in a one-liner, similar to the above*?
> 
> 
> You mean like this?
> 
> perl -pi -e 's/zope/foo/g;s/\n/ /' perf.txt
> 
> or did you mean something more complex?
> 
> -Ken

I got the impression from Randall that he was looking to combine the
lines together and then attempt to execute the replacement operator
(s//).

Given the case mentioned above, I would probably do something like:

perl -0e 'my $line = <>;$line =~ s/\n//g; $line =~ s/The quick/The not
so quick/; print $line;' words

or:

perl -0pi -e 's/\n//g; s/The quick/The not so quick/' words (if you
want to do in-place editing)

where words contains something like:

The 
quick 
brown fox 
jumped over the 
lazy 
dog.

In this case, the -0 operator allows you to specify the line-ending
operator (using an octal number), and if you leave it blank it
defaults to null.

But, you have to remember to strip the \n characters after reading the line.

In either case, I hope that helps.

> 
> 
> _______________________________________________
> Pdx-pm-list mailing list
> Pdx-pm-list at mail.pm.org
> http://mail.pm.org/mailman/listinfo/pdx-pm-list
> 


-- 
Ben Prew
ben.prew at gmail.com


More information about the Pdx-pm-list mailing list