Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > how to do that

Reply
Thread Tools

how to do that

 
 
Barti
Guest
Posts: n/a
 
      01-27-2004
I have lot of files in lot of dirs
I need change
[text]
into
['text']
but! text is not constans
so I ned find[*]
and change in to
['*']

barti
linux


 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      01-28-2004
Barti wrote:
> I have lot of files in lot of dirs


See perldoc File::Find

> I need change


See perldoc -q change
"How do I change one line in a file/delete a line in a file/insert a line in
th
e middle of a file/append to the beginning of a file?"

> [text]
> into
> ['text']
> but! text is not constans
> so I ned find
>[*]
> and change in to
> ['*']


See perldoc perlre
and perldoc perlretut

Actually, why don't you show us the code you have so far?
Then it would be much easier to find and fix any problems with it.

jue


 
Reply With Quote
 
 
 
 
Barti
Guest
Posts: n/a
 
      01-28-2004

> Actually, why don't you show us the code you have so far?
> Then it would be much easier to find and fix any problems with it.


To tell you the truth I have no code.
I need in my php files change this.
I do not want to do that one by one (too many hours!) and
I don't know perl at all but I know that perl with find can do that

I have somethig like this to change one word in another
find -type f -name '*.*' -exec perl -pi -e 's/word/anotherword/;' {} \;
but this situation is diffrent because I need find strings match pattern[*] where * is wahtever and change into ['*']




 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      01-28-2004
Barti wrote:
>> Actually, why don't you show us the code you have so far?
>> Then it would be much easier to find and fix any problems with it.

>
> To tell you the truth I have no code.
> I need in my php files change this.
> I do not want to do that one by one (too many hours!) and
> I don't know perl at all but I know that perl with find can do that
>
> I have somethig like this to change one word in another
> find -type f -name '*.*' -exec perl -pi -e 's/word/anotherword/;' {}
> \; but this situation is diffrent because I need find strings match
> pattern[*] where * is wahtever and change into ['*']


Again, "perldoc perlretut" is your friend.
<quote>
Extracting matches

The grouping metacharacters "()" also serve another completely different
function: they allow the extraction of the parts of a string that
matched. This is very useful to find out what matched and for text
processing in general. For each grouping, the part that matched inside
goes into the special variables "$1", "$2", etc. They can be used just
as ordinary variables:

# extract hours, minutes, seconds
$time =~ /(\d\d)\d\d)\d\d)/; # match hh:mm:ss format
$hours = $1;
$minutes = $2;
$seconds = $3;
</quote>

Just use that $1, $2, $3, ... in the substitution string.

jue


 
Reply With Quote
 
Oliver Frick
Guest
Posts: n/a
 
      02-05-2004
>
> I have somethig like this to change one word in another
> find -type f -name '*.*' -exec perl -pi -e 's/word/anotherword/;' {} \;
> but this situation is diffrent because I need find strings match pattern
>[*] where * is wahtever and change into ['*']
>


what you mean exactly, do you want do change, say [January] in ['January'],
[January 2003] in ['January 2004'], or change [whatever] into ['something
other'],
and how you want to use your script? Just with 2 words for search and
replace,
or more than this?




 
Reply With Quote
 
Aaron Sherman
Guest
Posts: n/a
 
      02-05-2004
"Barti" <(E-Mail Removed)> wrote in message news:<bv6pd1$hbf$(E-Mail Removed)>...
> I have lot of files in lot of dirs
> I need change
> [text]
> into
> ['text']


The simple answer is:

s/\[(.*?)\]/$1/g;

But, that's not really correct because you might be dealing with
nested brackets. If you are, then you need Text::Balanced. See:

http://www.perldoc.com/perl5.8.0/lib/Text/Balanced.html

Matching balanced characters like parens or brackets is easy in a CS
sense, but hard in practice in Perl and just about anything else but
Snobol. Text::Balanced bridges that gap.

Enjoy.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments