Re: Use file name in replace pattern

2010-02-14 Thread Marek Stepanek

On 14.02.2010 18:22, Marek Stepanek wrote:



Here a Perl solution:


> snip

I already said, I am not a Perl professional. Here a improved version:



#!/usr/bin/perl


use warnings;
use strict;
use File::Find;

my $start_dir = '/Users/path/to/start/folder';

find( \&wanted, $start_dir );

sub wanted {
return unless /\.txt$/;
my $file_name = $_;
my $file_path = $File::Find::name;
open (IN, "+<", $file_path);
my $out = "";
while () {
# s/$file_name//; # this is the way back for testing
s/this is file:\s*/$&$file_name/ig;
$out .= $_;
}
seek(IN, 0, 0) or die "can't seek to start of $file_path: $!";
print IN $out or die "can't print to $file_path: $!";
truncate (IN, tell(IN)) or die "can't truncate $file_path: $!";
close IN or die "can't close $file_path: $!";
}



--
___

the embassy for talented young musicians
   Podium International | Marek Stepanek | ms...@podiuminternational.org
http://www.PodiumInternational.org
___

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


Re: Use file name in replace pattern

2010-02-14 Thread Marek Stepanek



Here a Perl solution:


I am not very good in Perl and your question was too vague to make a 
script adapted for your needs.


I made several files in a certain folder containing "this is file:" This 
script adds to this the file name: "this is file: filename".




#!/usr/bin/perl


use warnings;
use strict;
use File::Find;

my $start_dir = '/Users/path/to/start/folder';

find( \&wanted, $start_dir );

sub wanted {
return unless /\.txt$/; # file extension you are searching for
my $file_path = $File::Find::name;
print "$file_path\n";
$file_path =~ m~^.*?/([^/]+)$~;
my $file_name = $1;
open (IN, "+<", $file_path);
my $out = "";
while () {
# s/$file_name//; # this is the way back, removes the file_names
s/this is file:\s*/$&$file_name/ig;
$out .= $_;
}
seek(IN, 0, 0) or die "can't seek to start of $file_path: $!";
print IN $out or die "can't print to $file_path: $!";
truncate (IN, tell(IN)) or die "can't truncate $file_path: $!";
close IN or die "can't close $file_path: $!";
}

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


Re: Use file name in replace pattern

2010-02-14 Thread Bucky Junior
jmichel,
I'm assuming from your question that you are able to find the line you
want to replace.

If you want to replace your find with the file name--hardcoded--I
would suspect that Applescript would be the choice of many, but I
can't seem to get anything to work for me with Applescript. I'm sure
it's my problem.

You might consider BBEdit's "Include" options, specifically the
persistent include mentioned on p. 380+ of my manual. For instance,
starting with a short html test file (as per the example)


    Include Test
    







When this file is Updated (Markup-> Update-> Document) the name of the
current file is inserted into the document. You can put this in any
file and it will update to include the document name. It depends on
the short Perl program placed (in this case the same directory as the
test file).
#!/usr/bin/perl -w
# saved as filenamefoo.pl
my $file = shift @ARGV;
print "Filename: $file\n";

Hope this helps. I'm sure there are other ways of accomplishing what
you want done.

Bucky

On Thu, Feb 11, 2010 at 5:49 PM, jmichel  wrote:
>
> Hi,
> Does anybody know whether it is somehow possible to include the
> current file name in a grep "replace" pattern when doing a multi-file
> search/replace. I have a case now where I need to replace one line in
> each of my files by a string containing the name of the file in which
> the line occurs.
> I am sure this can be done in unix using a sed script but I am not too
> familiar with the syntax, therefore I wondered whether BBEdit could do
> the job. Thanks…
>
> --
> 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.

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