1

I use perl regex capture groups to replace the pattern of a large number of files.

File example 1:

title="alpha" lorem ipsum lorem ipsum name="beta"

File example 2:

title="omega" Morbi posuere metus purus name="delta"

for

title="beta" lorem ipsum lorem ipsum
title="delta" Morbi posuere metus purus

using

find . -type f -exec perl -pi -w -e 's/title="(?'one'.*?)"(?'three'.*?)name="(?'two'.*?)"/title="\g{two}"\g{three}/g;' \{\} \;

(Note that (1) attribute values of title and name are unknown variables and (2) the content between title="alpha" and name="beta" differs. )

I am still learning perl regex. What am I doing wrong?

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
  • 1
    It looks to me like your Perl regex may be fine, but you're making life (more) difficult for yourself because you're mixing it up with shell quoting. The `'` around the code is interrupted by the `'` symbols around your `(?'identifier'pattern')` bits. Try using `(?pattern)` instead, like this? `s/title="(?.*?)"(?.*?)name="(?.*?)"/title="\g{two}"\g{three}/g;` – Erwin Jul 29 '22 at 18:30

1 Answers1

1

Your challenge is the shell quoting, but also the user of named capture groups. I'm not quite clear on whether you can use those the way you do, in the substitution.

This works to accomplish your task, as I understand it:

find . -type f -exec perl -pi -w -e 's/title="(.*?)"(.*?)name="(.*?)"/title="$3"$2/g;' \{\} \;

For instance:

$ cat file1
title="alpha" lorem ipsum lorem ipsum name="beta"

$ cat file2
title="omega" Morbi posuere metus purus name="delta"

Run the above line, and then:

$ cat file1
title="beta" lorem ipsum lorem ipsum

$ cat file2
title="delta" Morbi posuere metus purus

Hope that helps.

Erwin
  • 844
  • 4
  • 14
  • This is a good answer and the `shell` tag should be on this question. I rejected that but was wrong in doing so. – Andy Lester Jul 29 '22 at 19:55
  • https://stackoverflow.com/a/73168776/19301214 -- I think this is an even better answer, on the copied/duplicated question. – Erwin Jul 30 '22 at 00:01