Chetak Sasalu wrote: > > From: John W. Krahn [mailto:[EMAIL PROTECTED] > Sent: Saturday, December 20, 2003 3:33 PM > To: [EMAIL PROTECTED] > Subject: Re: How to write a page break character. > > > > Chetak Sasalu wrote: > > > > > > Hi, > > > > Hello, > > > > > I want to search for the word "status" in a group of files in a > > > directory and replace it with "status\n^L" where ^L is a page break > > > chatacter, In vi I can type it in using cntrl+l. > > > > > > I want to do this by > > > > > > perl -p -i.old -e 's/^STATUS$/STATUS\n(page break character)/' * > > > > > > How can I "write" the page break character (^L) on command line? > > > > Both perlop.pod and perlre.pod list the escape sequences that Perl (and > > C) use for printing control characters. > > > > perl -i.old -pe's/^STATUS$/STATUS\n\f/' * > > > > Or: > > > > perl -i.old -pe's/^STATUS$/STATUS\n\cL/' * > > > > Or: > > > > perl -i.old -pe's/^STATUS$/STATUS\n\014/' * > > > > Or: > > > > perl -i.old -pe's/^STATUS$/STATUS\n\x0C/' * > > I have one more requirement, The last STATUS which occurs just prior to > the file end should not be followed by a page break character. How can I > code this in perl? > > Thanks John! For giving me the fish and, more importantly, the fishing > net(pod) :-D
Hi Chetak. Unfortunately this isn't as easy as you might think, because you have to read up to the next STATUS line or the end of the file to discover whether the current STATUS is the last in the file. There are other ways, such as reading all through the file once first counting how many STATUS lines there are and then reading it again, printing a FF after all but the last. But since I don't know how big your file is I can't suggest that. The code below should work, although it's untested. I'll wait for you to come back with questions if you don't understand it straight off. It simply reads the file in chunks that /end/ with a line containing STATUS and prints "\n\f" before it unless it's the first chunk. use strict; use warnings; my $lines; my $chunk; local $^I = ''; while (<>) { $lines .= $_; next unless /^STATUS$/; print "\n\f" if $chunk++; print $lines; $lines = ''; } print $lines; HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>