Reputation: 2044
I cannot seem to make sed
do a regex replace with group capture in this example:
What I tried with sed
:
$ echo "DONE project 1" | sed -E 's/^DONE(.*)$/$1 DONE/g'
$1 DONE # fail! no group capture in `sed`?
Is there a way to do this in Perl
?
Actually this is part of an AppleScript used by DEVONthink[^1], and I realized that sed
was not able to do regex search/replace with group capture.
[^1]: DEVONthink's search/replace script with AppleScript usage
set transformedName to do shell script "echo " & quoted form of itemName & " | sed -E 's/" & sourcePattern & "/" & destPattern & "/g'"
set name of selectedItem to transformedName
Upvotes: 2
Views: 610
Reputation: 132822
The Perl version is very close to the sed
version because Perl stole some of sed
's features:
$ perl -pe 's/^DONE(.*)/\1 DONE/'
The -p
effectively wraps while(<>) { ...; print }
around your argument to -e
.
Note that the /g
flag doesn't make much sense here. You're going to match the first DONE
and everything after it. There's no second match to make.
Upvotes: 2
Reputation: 18611
sed
can easily do this, use \1
, $1
is the Perl backreference syntax:
sed -E 's/^DONE(.*)/\1 DONE/g'
.*
matches all the line up to the end, so you do not have to use $
in the LHS.
Upvotes: 2