如何使用sed替换regex捕获组?

时间:2021-11-15 16:50:41

I have a large file with many scattered file paths that look like

我有一个大文件,其中有许多分散的文件路径

lolsed_bulsh.png

I want to prepend these file names with an extended path like:

我想在这些文件名前面加上一个扩展路径,如:

/full/path/lolsed_bullsh.png

I'm having a hard time matching and capturing these. currently i'm trying variations of:

我很难匹配和捕捉这些。目前我正在尝试以下的变体:

cat myfile.txt| sed s/\(.+\)\.png/\/full\/path\/\1/g | ack /full/path

I think sed has some regex or capture group behavior I'm not understanding

我认为sed有一些regex或捕获组行为,我不理解

3 个解决方案

#1


5  

sed uses POSIX BRE, and BRE doesn't support one or more quantifier +. The quantifier + is only supported in POSIX ERE. However, POSIX sed uses BRE and has no option to switch to ERE.

sed使用POSIX BRE,而BRE不支持一个或多个量词+。量词+仅在POSIX ERE中被支持。然而,POSIX sed使用BRE,无法切换到ERE。

Use ..* to simulate .+ if you want to maintain portability.

使用. .*模拟。+如果你想保持可移植性。

Or if you can assume that the code is always run on GNU sed, you can use GNU extension .\+. Alternatively, you can also use the GNU extension -r flag to switch to POSIX ERE. The -E flag in higuaro's answer is an undocumented flag for compatibility with BSD sed with the same effect.

或者,如果您可以假设代码总是在GNU sed上运行,那么您可以使用GNU扩展。\+。或者,也可以使用GNU扩展-r标志切换到POSIX ERE。在higuaro的答案中,-E标志是一个与BSD兼容的无文档标志,具有相同的效果。

#2


10  

In your regex change + with *:

在您的regex更改+ with *中:

sed -E "s/(.*)\.png/\/full\/path\/\1/g" <<< "lolsed_bulsh.png"

It prints:

它打印:

/full/path/lolsed_bulsh

NOTE: The non standard -E option is to avoid escaping ( and )

注意:非标准的-E选项是避免转义(和)

#3


1  

Save yourself some escaping by choosing a different separator (and -E option), for example:

通过选择不同的分隔符(和-E选项)为自己节省一些转义,例如:

cat myfile.txt | sed -E "s|(..*)\.png|/full/path/\1|g" | ack /full/path

#1


5  

sed uses POSIX BRE, and BRE doesn't support one or more quantifier +. The quantifier + is only supported in POSIX ERE. However, POSIX sed uses BRE and has no option to switch to ERE.

sed使用POSIX BRE,而BRE不支持一个或多个量词+。量词+仅在POSIX ERE中被支持。然而,POSIX sed使用BRE,无法切换到ERE。

Use ..* to simulate .+ if you want to maintain portability.

使用. .*模拟。+如果你想保持可移植性。

Or if you can assume that the code is always run on GNU sed, you can use GNU extension .\+. Alternatively, you can also use the GNU extension -r flag to switch to POSIX ERE. The -E flag in higuaro's answer is an undocumented flag for compatibility with BSD sed with the same effect.

或者,如果您可以假设代码总是在GNU sed上运行,那么您可以使用GNU扩展。\+。或者,也可以使用GNU扩展-r标志切换到POSIX ERE。在higuaro的答案中,-E标志是一个与BSD兼容的无文档标志,具有相同的效果。

#2


10  

In your regex change + with *:

在您的regex更改+ with *中:

sed -E "s/(.*)\.png/\/full\/path\/\1/g" <<< "lolsed_bulsh.png"

It prints:

它打印:

/full/path/lolsed_bulsh

NOTE: The non standard -E option is to avoid escaping ( and )

注意:非标准的-E选项是避免转义(和)

#3


1  

Save yourself some escaping by choosing a different separator (and -E option), for example:

通过选择不同的分隔符(和-E选项)为自己节省一些转义,例如:

cat myfile.txt | sed -E "s|(..*)\.png|/full/path/\1|g" | ack /full/path