how to do that

Discussion in 'Perl' started by Barti, Jan 27, 2004.

  1. Barti

    Barti Guest

    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
     
    Barti, Jan 27, 2004
    #1
    1. Advertising

  2. 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
     
    Jürgen Exner, Jan 28, 2004
    #2
    1. Advertising

  3. Barti

    Barti Guest


    > 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 ['*']
     
    Barti, Jan 28, 2004
    #3
  4. 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
     
    Jürgen Exner, Jan 28, 2004
    #4
  5. Barti

    Oliver Frick Guest

    >
    > 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?
     
    Oliver Frick, Feb 5, 2004
    #5
  6. "Barti" <> wrote in message news:<bv6pd1$hbf$>...
    > 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.
     
    Aaron Sherman, Feb 5, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page