我有一个 bash 变量、一些文件路径(带空格)和文件名,例如:
$ echo $tmp
/home/xyz/some/path/with spaces/AlbumArt_{random-number-sequence}_Large.jpg
当我尝试使用 grep 识别文件名部分时,例如:
$ echo "$tmp" | egrep 'AlbumArt.*Large.jpe?g$'
/home/xyz/some/path/with spaces/**AlbumArt_{random-number-sequence}_Large.jpg**
文件名部分似乎被正确识别,但是当我尝试将其转换为 sed 替换表达式时,例如:
$ echo "$tmp" | sed 's@AlbumArt.*Large.jpe?g$@NewString@'
/home/xyz/some/path/with spaces/AlbumArt_{random-number-sequence}_Large.jpg
预期的替代并未发生。预先感谢您的帮助。
事实上 egrep 是 grep -E 的变体,允许“激活”扩展正则表达式(您可以查看:https://en.wikipedia.org/wiki/Regular_expression#Standards)。
因此,您只需对 sed 使用相同的选项即可:
echo "$tmp" | sed -E 's@AlbumArt.*Large.jpe?g$@NewString@'