recursive replace command line

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default recursive replace command line

    Hi -

    giving my perl a retry, I found some hints on a website to recursively
    replace text

    perl -p -i -e 's/old\(.\)atext/new\1btext/g;' $( find ./ -name '*.html' -o -name '*.txt' )

    this should replace "old-atext" with "new-btext" and "old+atext" with
    "new+btext" and similar occurrences in all the files from find.

    but from what I can tell, perl doesn't support the \1 for \(*\) symbols
    like sed does. What is the work around?

    // George


    --
    GEORGE GEORGALIS, System Admin/Architect cell: 646-331-2027 <IXOYE><
    Security Services, Web, Mail, mailto:george@galis.org
    Multimedia, DB, DNS and Metrics. [url]http://www.galis.org/george[/url]

    George Georgalis Guest

  2. Similar Questions and Discussions

    1. #40715 [NEW]: level too deep - recursive dependency? in Unknown on line 0
      From: shoichi at cf7 dot so-net dot ne dot jp Operating system: CentOS4.4 PHP version: 5.2.1 PHP Bug Type: Scripting Engine...
    2. command line search and replace
      Hi all, When I try the following, perl reads the * as a literal character, instead of my intent as a global value. Anyone see what I am missing? ...
    3. RUN/execute a Command-Line command from an ASP page
      Hi, I need to RUN/execute a Command-Line command from an ASP page. This is the command: sse45.exe -i k:\o\2.wmv -o k:\o\2.shh -w 128 -df 0 -m 2...
    4. RUN/execute a Command-Line command from an ASP page.
      Hi, I need to RUN/execute a Command-Line command from an ASP page. This is the command: sse45.exe -i k:\o\2.wmv -o k:\o\2.shh -w 128 -df 0 -m 2...
  3. #2

    Default Re: recursive replace command line

    On Dec 10, George Georgalis said:
    >giving my perl a retry, I found some hints on a website to recursively
    >replace text
    >
    >perl -p -i -e 's/old\(.\)atext/new\1btext/g;' $( find ./ -name '*.html' -o -name '*.txt' )
    This isn't recursively replacing text; it's recursively going through a
    directory tree.
    >but from what I can tell, perl doesn't support the \1 for \(*\) symbols
    >like sed does. What is the work around?
    Because Perl is not sed. Perl uses (...), not \(...\) for its memory
    capturing. In Perl's regexes, all non-alphanumeric metacharacters don't
    use backslashes. That means [...] for character classes, not \[...\], and
    + for " 1 or more", not \+, and so on.

    --
    Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

  4. #3

    Default Re: recursive replace command line

    On Thu, Dec 11, 2003 at 09:05:20AM -0500, Jeff 'japhy' Pinyan wrote:
    >On Dec 10, George Georgalis said:
    >
    >>giving my perl a retry, I found some hints on a website to recursively
    >>replace text
    >>
    >>perl -p -i -e 's/old\(.\)atext/new\1btext/g;' $( find ./ -name '*.html' -o -name '*.txt' )
    >
    >This isn't recursively replacing text; it's recursively going through a
    >directory tree.
    >
    >>but from what I can tell, perl doesn't support the \1 for \(*\) symbols
    >>like sed does. What is the work around?
    >
    >Because Perl is not sed. Perl uses (...), not \(...\) for its memory
    >capturing. In Perl's regexes, all non-alphanumeric metacharacters don't
    >use backslashes. That means [...] for character classes, not \[...\], and
    >+ for " 1 or more", not \+, and so on.
    that's what I needed to hear... however replacing text (with memory
    capturing) is still a problem:

    perl -p -i -e 's/451(.)8229/331\12027/g;' $( find ./ -type f -name '*.html' -o -name '*.txt' )

    here I'm trying to replace instances of of a phone number, that might
    use "-" " " or "." to separate the first and second parts.
    451-8229 to 331-2027
    451 8229 to 331 2027
    etc

    the result "331P27" hinted 3 digits may be used for the call back

    perl -p -i -e 's/451(.)8229/331\0012027/g;' $( find ./ -type f -name '*.html' -o -name '*.txt' )

    that puts the characters where they go but the dash has changed to some
    other character (an ascII 001 I suppose).

    // George


    --
    GEORGE GEORGALIS, System Admin/Architect cell: 646-331-2027 <IXOYE><
    Security Services, Web, Mail, mailto:george@galis.org
    Multimedia, DB, DNS and Metrics. [url]http://www.galis.org/george[/url]

    George Georgalis Guest

  5. #4

    Default Re: recursive replace command line

    On Dec 11, George Georgalis said:
    >On Thu, Dec 11, 2003 at 09:05:20AM -0500, Jeff 'japhy' Pinyan wrote:
    >>On Dec 10, George Georgalis said:
    >>
    >>>giving my perl a retry, I found some hints on a website to recursively
    >>>replace text
    >>>
    >>>perl -p -i -e 's/old\(.\)atext/new\1btext/g;' $( find ./ -name '*.html' -o -name '*.txt' )
    >>
    >>This isn't recursively replacing text; it's recursively going through a
    >>directory tree.
    >>
    >>>but from what I can tell, perl doesn't support the \1 for \(*\) symbols
    >>>like sed does. What is the work around?
    >>
    >>Because Perl is not sed. Perl uses (...), not \(...\) for its memory
    >>capturing. In Perl's regexes, all non-alphanumeric metacharacters don't
    >>use backslashes. That means [...] for character classes, not \[...\], and
    >>+ for " 1 or more", not \+, and so on.
    >
    >that's what I needed to hear... however replacing text (with memory
    >capturing) is still a problem:
    >
    >perl -p -i -e 's/451(.)8229/331\12027/g;' $( find ./ -type f -name '*.html' -o -name '*.txt' )
    Right; first of all, \1 should only be used IN the regex ITSELF. Outside
    of the regex, you should use $1. However, "331$12027" still isn't
    appropriate; you'll need "331${1}2027". The reason "331\12027" gave you
    "331P27" is because octal character 120 is "P".

    --
    Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

  6. #5

    Default Re: recursive replace command line

    On Thu, Dec 11, 2003 at 11:21:39AM -0500, Jeff 'japhy' Pinyan wrote:
    >On Dec 11, George Georgalis said:
    >
    >>On Thu, Dec 11, 2003 at 09:05:20AM -0500, Jeff 'japhy' Pinyan wrote:
    >>>On Dec 10, George Georgalis said:
    >>>
    >>>>giving my perl a retry, I found some hints on a website to recursively
    >>>>replace text
    >>>>
    >>>>perl -p -i -e 's/old\(.\)atext/new\1btext/g;' $( find ./ -name '*.html' -o -name '*.txt' )
    >>>
    >>>This isn't recursively replacing text; it's recursively going through a
    >>>directory tree.
    >>>
    >>>>but from what I can tell, perl doesn't support the \1 for \(*\) symbols
    >>>>like sed does. What is the work around?
    >>>
    >>>Because Perl is not sed. Perl uses (...), not \(...\) for its memory
    >>>capturing. In Perl's regexes, all non-alphanumeric metacharacters don't
    >>>use backslashes. That means [...] for character classes, not \[...\], and
    >>>+ for " 1 or more", not \+, and so on.
    >>
    >>that's what I needed to hear... however replacing text (with memory
    >>capturing) is still a problem:
    >>
    >>perl -p -i -e 's/451(.)8229/331\12027/g;' $( find ./ -type f -name '*.html' -o -name '*.txt' )
    >
    >Right; first of all, \1 should only be used IN the regex ITSELF. Outside
    >of the regex, you should use $1. However, "331$12027" still isn't
    >appropriate; you'll need "331${1}2027". The reason "331\12027" gave you
    >"331P27" is because octal character 120 is "P".
    Thanks,

    perl -p -i -e 's/347(.)451(.)8229/646${1}331${2}2027/g;' $( find ./ -type f -name '*.html' -o -name '*.txt' )
    perl -p -i -e 's/347(..)451(.)8229/646${1}331${2}2027/g;' $( find ./ -type f -name '*.html' -o -name '*.txt' )

    worked perfect to update my web pages... :)
    btw - what's the best manpage for the perl command line options?

    TAI,

    // George


    --
    GEORGE GEORGALIS, System Admin/Architect cell: 646-331-2027 <IXOYE><
    Security Services, Web, Mail, mailto:george@galis.org
    Multimedia, DB, DNS and Metrics. [url]http://www.galis.org/george[/url]

    George Georgalis Guest

  7. #6

    Default Re: recursive replace command line

    George Georgalis wrote:
    >
    > that's what I needed to hear... however replacing text (with memory
    > capturing) is still a problem:
    >
    > perl -p -i -e 's/451(.)8229/331\12027/g;' $( find ./ -type f -name '*.html' -o -name '*.txt' )
    Hi George,

    If you are going to do your regex work--especially at a high level--in Perl, it is imperative that
    you read the Perl documentation. The problem in the above sam[ple, or the one that jumps out at me,
    is that you are using the backslash form of back reference in the replacement string. In the Perl
    implementation, that is not appropriate. Use the built-in scalars ($1, $2 ...) instead. Backslash
    backreferences are used within the scanning portion of the regex, but not in the replqacement
    string.

    Please read:
    perldoc perlre

    Joseph

    R. Joseph Newton Guest

  8. #7

    Default Re: recursive replace command line

    On Thu, Dec 11, 2003 at 11:07:53AM -0800, R. Joseph Newton wrote:
    >If you are going to do your regex work--especially at a high level--in Perl, it is imperative that
    >you read the Perl documentation. The problem in the above sam[ple, or the one that jumps out at me,
    >is that you are using the backslash form of back reference in the replacement string. In the Perl
    >implementation, that is not appropriate. Use the built-in scalars ($1, $2 ...) instead. Backslash
    >backreferences are used within the scanning portion of the regex, but not in the replqacement
    >string.
    >
    >Please read:
    >perldoc perlre
    Thanks for the pointer, Joseph. Indeed, I'm not yet up to speed on
    accessing perl-doc, there is quite a lot. ;-)

    Bye,
    // George

    --
    GEORGE GEORGALIS, System Admin/Architect cell: 646-331-2027 <IXOYE><
    Security Services, Web, Mail, mailto:george@galis.org
    Multimedia, DB, DNS and Metrics. [url]http://www.galis.org/george[/url]

    George Georgalis Guest

  9. #8

    Default Re: recursive replace command line

    On Thu, Dec 11, 2003 at 12:17:03PM -0500, George Georgalis wrote:
    > btw - what's the best manpage for the perl command line options?
    perlrun

    --
    Paul Johnson - [email]paul@pjcj.net[/email]
    [url]http://www.pjcj.net[/url]
    Paul Johnson Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139