Re: Replace on Different Line Than Find?

2010-11-02 Thread Warren Michelsen
At 8:42 AM +0100 11/2/10, John Delacour sent email regarding Re: 
Replace on Different Line Than Find?:


Here's a way to do it using a UNIX filter on an open BBEdit document:

(Filename: "p.pl")

#!/usr/bin/perl
use strict;
my $year;
while (<>){
  m~^(movieY.+)([0-9][0-9][0-9][0-9])[ \t]?~i and $year = " ($2)";
  s~^(title.*)~$1$year~;
  print;
}


As expected, the above works as a Unix filter in BBEdit.

This line:
m~^(movieY.+)([0-9][0-9][0-9][0-9])[ \t]?~i and $year = " ($2)";
appears to set the var $year to the four digits within parentheses. 
What does the "[ \t]?~i " part do?


In terminal, the output goes to STDOUT. How would I get it to replace 
in place, updating the file?


--
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>


Re: Replace on Different Line Than Find?

2010-11-02 Thread Warren Michelsen
At 8:42 AM +0100 11/2/10, John Delacour sent email regarding Re: 
Replace on Different Line Than Find?:


Here's a way to do it using a UNIX filter on an open BBEdit document:

(Filename: "p.pl")

#!/usr/bin/perl
use strict;
my $year;
while (<>){
  m~^(movieY.+)([0-9][0-9][0-9][0-9])[ \t]?~i and $year = " ($2)";
  s~^(title.*)~$1$year~;
  print;
}


Once I figured out the BBEdit Find/Replace, I saved the RegEx within 
BBEdit's Find dialog. Calling from a Unix filter will be a tad more 
convenient. Thanks.


I'll also turn it into an AppleScript droplet. That's my goal.

--
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>


Re: Replace on Different Line Than Find?

2010-11-02 Thread Warren Michelsen
At 8:41 PM -0600 11/1/10, Doug McNutt sent email regarding Re: 
Replace on Different Line Than Find?:


 I confess that I don't know how BBEdit currently handles filters 
that run in perl.  ...


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


I use a number of Perl scripts that are embedded within AppleScrip 
wrappers so I can drag and drop files onto them for processing. So 
far though, they've all been simple find/replace. For example:


#!/usr/bin/perl -i

use strict;
use warnings;

local $/;
while ($_ = <>) {
#if ($string =~ m/seriesID/)
  {
s/seriesID/seriesId/m;
s/\ =\ /\ :\ /mg;
  }

  print;
}

The above script is in the app bundle that calls the script just once 
with all the dropped files concatenated:


-- We call the perl script one time with the paths of all dropped files
-- concatenated to the command line.

on open dropped_items
	set perlScript to quoted form of (POSIX path of (path to me) & 
"Contents/Resources/metadata.pl")

set commandLine to perlScript
repeat with this_item in dropped_items
-- we don't want to process .mpg files dropped inadvertently
if (POSIX path of this_item) ends with ".txt" then
-- add this dropped text file to the command line
			set commandLine to commandLine & " " & quoted form of POSIX 
path of this_item

end if
end repeat
do shell script commandLine
end open

I have some other AppleScripts that loop through the dropped files, 
calling the internal Perl script once for each dropped file.






The < redirection operator tells the shell to use the file following 
it to be given to the perl filter as standard input.


AH! I wondered what that was. It's not used in any of my Drag-n-Drop scripts.

Usually, once I can figure out the GREP, I can cobble together 
another AS droplet based on one of my current, functional ones. My 
Perl knowledge is of the "Monkey see, monkey do" variety so I clone 
one of my scripts that works and modify as needed.


The RegX had me stumped for a while because it seemed like two 
separate searches, one to find the four digits, another to find the 
line to which the digits would be appended.


Then I realized that the digits always come before the target line, 
so it can all be done with one search / replace.


Thanks, I'm sure your examples will help.





--
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>


Re: Replace on Different Line Than Find?

2010-11-02 Thread John Delacour

At 07:58 -0700 01/11/2010, Warren Michelsen wrote:

Within each file passed to the Perl script, I want to find the line 
beginning "movieYear : " and get the four digits following the space 
and before the line end character.



^movieYear : (\d{4})

I need to append a space and the four digits (within parentheses) to 
the end of the line beginning "title : "


So, if I call the Perl script:

/path/to/perl/script.pl /path/to/Phantom.txt /path/to/Kong.txt

and if the files passed to the script contain among their lines

Phantom.txt
some text
More text
movieYear : 1925
additional lines of text
title : The Phantom of the Opera
more lines ...



Here's a way to do it using a UNIX filter on an open BBEdit document:

(Filename: "p.pl")

#!/usr/bin/perl
use strict;
my $year;
while (<>){
  m~^(movieY.+)([0-9][0-9][0-9][0-9])[ \t]?~i and $year = " ($2)";
  s~^(title.*)~$1$year~;
  print;
}


In the Terminal
perl p.pl a.txt
will print the result to STDOUT.




JD

--
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

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: 


Re: Replace on Different Line Than Find?

2010-11-01 Thread Doug McNutt
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 = ;  # 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 = ;
#  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>


Re: Replace on Different Line Than Find?

2010-11-01 Thread Warren Michelsen
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 = ;  # 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

?






Call perl like this.
perl -w filename.pl < filename.pl

Phantom.txt
some text
More text
movieYear : 1925
additional lines of text
title : The Phantom of the Opera
more lines

Kong.txt
some text
More text
movieYear : 1933
additional lines of text
title : King Kong
more lines



--
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>


Re: Replace on Different Line Than Find?

2010-11-01 Thread Doug McNutt
!/usr/bin/perl
undef $/; # Tell perl to ignore line ends in the input.
$thetext = ;  # 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__


Call perl like this.
perl -w filename.pl < filename.pl


Phantom.txt
some text
More text
movieYear : 1925
additional lines of text
title : The Phantom of the Opera
more lines


Kong.txt
some text
More text
movieYear : 1933
additional lines of text
title : King Kong
more lines


-- 

--> From the U S of A, the only socialist country that refuses to admit it. <--

-- 
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

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: 


Re: Replace on Different Line Than Find?

2010-11-01 Thread Warren Michelsen
At 7:58 AM -0700 11/1/10, Warren Michelsen sent email regarding 
Replace on Different Line Than Find?:
Is it possible within BBEdit to do a Find getting text from one line 
and them do another find/replace to append the original found text 
to the second found line? I.e., can BBEdit hold found text for later 
use?


If not, then I need some Perl help.



It looks like, in the files I need to process, the movieYear line 
always precedes the title line so that I can use BBEdit to:




Find:
(^movieYear : )(\d{4})((?s).+)(^title : )(.*$)

Replace
\1\2\3\4\5 \(\2\)

How would I express this particular find/replace in a Perl script?





Within each file passed to the Perl script, I want to find the line 
beginning "movieYear : " and get the four digits following the space 
and before the line end character.


^movieYear : (\d{4})

I need to append a space and the four digits (within parentheses) to 
the end of the line beginning "title : "


So, if I call the Perl script:

/path/to/perl/script.pl /path/to/Phantom.txt /path/to/Kong.txt

and if the files passed to the script contain among their lines



Phantom.txt
some text
More text
movieYear : 1925
additional lines of text
title : The Phantom of the Opera
more lines


Kong.txt
some text
More text
movieYear : 1933
additional lines of text
title : King Kong
more lines


I need the script to produce:


Phantom.txt
some text
More text
movieYear : 1925
additional lines of text
title : The Phantom of the Opera (1925)
more lines


Kong.txt
some text
More text
movieYear : 1933
additional lines of text
title : King Kong (1933)
more lines

Anyone care to lend a hand?



--
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>


Replace on Different Line Than Find?

2010-11-01 Thread Warren Michelsen
Is it possible within BBEdit to do a Find getting text from one line and them 
do another find/replace to append the original found text to the second found 
line? I.e., can BBEdit hold found text for later use?

If not, then I need some Perl help. 

Within each file passed to the Perl script, I want to find the line beginning 
"movieYear : " and get the four digits following the space and before the line 
end character.


^movieYear : (\d{4})

I need to append a space and the four digits (within parentheses) to the end of 
the line beginning "title : "

So, if I call the Perl script:

/path/to/perl/script.pl /path/to/Phantom.txt /path/to/Kong.txt

and if the files passed to the script contain among their lines



Phantom.txt
some text
More text
movieYear : 1925
additional lines of text
title : The Phantom of the Opera
more lines


Kong.txt
some text
More text
movieYear : 1933
additional lines of text
title : King Kong
more lines


I need the script to produce:


Phantom.txt
some text
More text
movieYear : 1925
additional lines of text
title : The Phantom of the Opera (1925)
more lines


Kong.txt
some text
More text
movieYear : 1933
additional lines of text
title : King Kong (1933)
more lines


Anyone care to lend a hand?


-- 
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

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: