At 14:27 -0700 11/1/10, Warren Michelsen wrote:
>At 1:05 PM -0600 11/1/10, Doug McNutt sent email regarding Re: Replace on 
>Different Line Than Find?:
>>!/usr/bin/perl
>>undef $/; # Tell perl to ignore line ends in the input.
>>$thetext = <STDIN>;  # read the whole document from standard input.
>>$e = "\n";  # Make it clear which line ends your document has in it. You 
>>might want "\r" or "\r\n".
>># Do the substitutions (s///) using the s flag to include line ends and the g 
>>flag to repeat.
>>#begin single line that hopefully didn't get shortened by email.
>>$thetext =~ s/${e}movieYear : (\d\d\d\d)${e}(.*?)${e}title : ([\w 
>>]*?)${e}/${e}movieYear : $1${e}$2${e}title : $3 \($1\)${e}/sg;
>>#end single line
>>print $thetext;  # report the result to standard output.
>>__END__
>
>I'm sure this will work splendidly, if only I knew what you were saying...
>
>So, if I save the above script as TitleYear.pl and have two text files to 
>process, Phantom.txt and Kong.txt, how, exactly, would I invoke it?
>
>path/to/TitleYear.pl -w Phantom.txt Kong.txt
>
***

 I confess that I don't know how BBEdit currently handles filters that run in 
perl.  I'm stuck on Mac OS neXt 10.3.9 because I refuse to give up my SE/30 
file server that needs Apple file sharing over ethernet. The result is that I'm 
way behind in BBEdit versions. (Confession:  I really like gedit under Ubuntu 
Linux. What I don't like I can change!)

If you use an ordinary Terminal session with the perl code named as you 
indicate you would first make sure your working directory is set with chdir and 
then enter:

perl -w  TitleYear.pl  <  Phantom.txt
# -   and later. . .
perl -w  TitleYear.pl  <  Kong.txt

The < redirection operator tells the shell to use the file following it to be 
given to the perl filter as standard input. The results are being sent to 
standard output which will appear on the lines below your command in the shell. 
You could copy and paste them into BBEdit but I'm pretty sure there is a way to 
do that within BBEdit so that it replaces the content of an open file. Can 
someone help with that?

You could also do something like:
perl   TitleYear.pl  <  Phantom.txt   > FixedFiles.txt
perl   TitleYear.pl  <  Kong.txt  >> FixedFiles.txt

which would redirect the first line's output to a new file FixedFiles.txt and 
add the second lines output to the file created by the first.

You shouldn't need the -w flag in the call to perl after you're satisfied that 
the code runs without error. It's just asking that warnings be displayed.

cat is the UNIX tool that concatenates files. Another option would be:

cat Phantom.txt  Kong.txt | perl  TitleYear.pl > Concatenated.txt

The | (pipe) operator says to pass the output of the cat program to the new 
perl tool as standard input.

You could also modify the perl code to pick up a list of files such as 
everything in the current directory that ends with .txt. That would involve 
looping over a list of arguments while running print statements to a single 
output file. That's a bit like your pathto line above.  Untested, because I'm 
preparing this on a Mac 8500 running OS 9, 

while (@argv)
    {
    $nextfile = shift @argv;  #get the next file in the list of arguments
    open IN, $nextfile;
    $thetext = <IN>;
#  do the above work as on a single file
    print $thetext;
    {

Your pathto line would be assuming that  TitleYear.pl is executable. The 
shebang (#!) line can make that work but you'll have to set the x permission on 
the perl file to make it work.

chmod +x  path/to/TitleYear.pl

is the appropriate shell  command.

perl is not so hard to learn. It helps if you know some C but learning perl can 
help make you into a C programmer. Lots of books and internet content are 
available.
-- 

-->  Halloween  == Oct 31 == Dec 25 == Christmas  <--

-- 
You received this message because you are subscribed to the 
"BBEdit Talk" discussion group on Google Groups.
To post to this group, send email to bbedit@googlegroups.com
To unsubscribe from this group, send email to
bbedit+unsubscr...@googlegroups.com
For more options, visit this group at
<http://groups.google.com/group/bbedit?hl=en>
If you have a feature request or would like to report a problem, 
please email "supp...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>

Reply via email to