do shell script Sed 和 Perl:使用 & 作为匹配的字符串

do shell script Sed and Perl: Using & as the matched string

输入:abc720p

期望的输出:abc720p_xxx

遵循指南:Using & as the matched string

在 AppleScript 中,如果我使用 sed:

set oriT to "abc720p def720p" 
set oriT to do shell script "echo " & 
quoted form of oriT & " | sed -e 's/abc720p/&_xxx/g'"

输出正确:abc720p_xxx def720p

但是如果我使用 Perl:

set oriT to "abc720p def720p" 
set oriT to do shell script "echo " &
quoted form of oriT & " | perl -CIOED -p -e 's/abc720p/&_xxx/g'"

输出错误:&_xxx def720p

在我的上下文中,我必须在 AS 中使用 Perl 来完成这项工作(就像在 sed 中的 &)。 我不能使用 "perl -CIOED -p -e 's/abc720p/abc720p_xxx/g".

我不知道我是否可以用 Perl 实现这个。

在 Perl 中,语法略有不同:使用 $& 而不是 &

perl -CIOED -p -e 's/abc720p/$&_xxx/g'

详情见$&

使用 AppleScript 更简单:

set oriT to "abc720p def720p"
set |&| to "abc720p"

set my text item delimiters to {|&| & "_xxx", |&|}

set oriT to text items of oriT as text
    --> "abc720p_xxx def720p"