简体   繁体   English

sed replace filename以引号开头

[英]sed replace filename starts with quote

I have in the file strings like img src="../image/popup.jpg" , or bg: 'images/xxx/popup.png' . 我在文件字符串中有img src="../image/popup.jpg"bg: 'images/xxx/popup.png' I would like to replace the entire filepath/name with another string with sed , I tried 我想用sed将另一个字符串替换整个文件路径/名称,我尝试过

sed 's/^"[^\s]*/somethingelse/g' 

试试这个,

 sed 's|/some/UNIX/path|/a/new/path|g' files

When using sed to substitute pathnames, changing its default demiliter helps, as noted by Skynet, and judicious use of single quotes allows variable interpolation like this: 如Skynet所述,当使用sed替换路径名时,更改其默认的半音符号会有所帮助,并且明智地使用单引号允许变量插值如下:

> a="\"hello/bye\""
> b="\"one/two\""
> echo $a
"hello/bye"
> echo $b
"one/two"
> echo $a|sed -e 's|'$a'|'$b'|'
"one/two"

From http://linux.die.net/man/1/sed sed does not support POSIX. http://linux.die.net/man/1/sed sed不支持POSIX。 2 basic regular expressions "completely because of performance problems", however it comes close. 2个基本正则表达式“完全由于性能问题”,但是非常接近。 According to http://www.regular-expressions.info/posix.html , one of the rules of POSIX regexes is "Using a backslash to escape a character that is never a metacharacter is an error." 根据http://www.regular-expressions.info/posix.html,POSIX正则表达式的规则之一是“使用反斜杠转义从不属于元字符的字符是错误的。” Since double and single quotes are not metacharacters, they should not be backslashed in POSIX regexes according to this. 由于双引号和单引号不是元字符,因此不应在POSIX正则表达式中将它们加反斜杠。

Using sed with BRE: 结合使用sed和BRE:

sed "s/\([\"']\)[^\"']*\([\"']\)/\1somethingelse\2/g" file

With ERE: 使用ERE:

sed -r "s/([\"'])[^\"']*([\"'])/\1somethingelse\2/g" file

Assumed there's no escaped quote in filename, ie no quote inside "..." and '...' 假设文件名中没有转义的引号,即"..."'...'内没有引号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM