简体   繁体   English

sed编辑文件上的一行

[英]sed to edit a line on a file

I have a line value I need to update on a file, 我有一个需要在文件上更新的行值,

export const ADDRESS_ORIGIN = 'chupa-cabra.muz.id';

Im tryging to update with: 我正在尝试更新:

sed -i '' 's/ADDRESS_ORIGIN ="[0-9.]*"/ADDRESS_ORIGIN ="'chapuza'"/' constants.js

But the value for 但是对于

ADDRESS_ORIGIN ADDRESS_ORIGIN

Is not updating, I think my REGEX is very wrong, how can I update the value? 不更新,我认为我的REGEX非常错误,如何更新值?

Thanks! 谢谢!

There is no way "[0-9.]*" can match 'chupa-cabra.muz.id' but without knowing what the permitted values look like we can only speculate. 无法使用"[0-9.]*"匹配'chupa-cabra.muz.id'但不知道允许的值是什么样,我们只能推测。 Perhaps '[^']*' would do what you want, though you need to understand the mechanisms of shell quoting in order to correctly get it through to sed . 也许'[^']*'会做您想要的事情,尽管您需要了解shell引用的机制以便正确地将其传递给sed In this particular case, the simplest by far is to use double quotes around the sed script instead of single: 在这种情况下,到目前为止,最简单的方法是在sed脚本周围使用双引号而不是单引号:

sed -i '' "s/ADDRESS_ORIGIN = *'[^']*'/ADDRESS_ORIGIN = 'chapuza'/" constants.js

though if you know what you are doing, you could also do it with alternating single and double quotes: 但是,如果您知道自己在做什么,也可以使用单引号和双引号交替进行:

sed -i '' 's/ADDRESS_ORIGIN = *'"'[^']*'"'/ADDRESS_ORIGIN = '"'chapuza'/" constants.js

The basic mechanism here is that adjacent strings will be glued together into one string by the shell. 此处的基本机制是,相邻的弦将通过外壳粘合在一起成为一个弦。 So "a"'b' is aa double-quoted a followed by a single-quoted b . 因此, "a"'b'是a的双引号a后跟的单引号b After the shell is done parsing it, you get the string ab . Shell完成解析后,您将获得字符串ab Now for fun, imagine that a is a literal single quote, and b is a literal double quote. 现在,为了好玩,假设a是字面单引号, b是字面双引号。

... Or perhaps you only want to replace the first token before the dot? ...也许您只想替换点号之前的第一个标记? Then '[^.']*\\. 然后是'[^.']*\\. would match and you'd want to replace with 'chapuza. 会匹配,并且您想替换为'chapuza. Or use a back reference '[^.']*\\(['.]\\) and replace with 'chapuza\\1 或使用向后引用'[^.']*\\(['.]\\)并替换为'chapuza\\1

In the general case, of course, double quotes have different semantics than single, so if your script uses characters which will be mangled by the shell when you switch quotes, you need to add a backslash before them, or switch to the "seesaw quoting" mechanism I described above. 当然,在一般情况下,双引号的语义与单引号不同,因此,如果您的脚本使用的字符在切换引号时会被外壳破坏,则需要在它们之前添加反斜杠,或切换到“跷跷板引号”我在上面介绍了这种机制。 But in this particular case, your example script doesn't require any modifications to adapt to double quotes. 但是在这种特殊情况下,示例脚本不需要进行任何修改即可适应双引号。

I assume you are on macOS because of the way you are using in-place editing; 由于您使用就地编辑的方式,我认为您使用的是macOS this should work : 这应该工作

sed -E -i '' "s@(ADDRESS_ORIGIN = )'.*'@\1'chapuza'@g"

Result : 结果

export const ADDRESS_ORIGIN = 'chapuza';

Why should it? 为什么要这样
You missed the space after the equal sign, and double quotes " will literally match double quotes not single quotes ' , and [0-9.]* will match consecutive digits and dots. 您错过了等号后的空格,并且双引号"将字面上匹配双引号而不是单引号' ,并且[0-9.]*将匹配连续的数字和点。

An alternative to the bewildering quotes alternating would be use ASCII inside the regex: 令人困惑的引号交替的替代方法是在正则表达式中使用ASCII:

sed 's/ADDRESS_ORIGIN *= *\x27[^\x27]*\x27/ADDRESS_ORIGIN = \x27chapuza\x27/'

or 要么

sed 's/\(ADDRESS_ORIGIN\) *= *\x27[^\x27]*\x27/\1 = \x27chapuza\x27/'

Or better still, since there could be double quotes around the value, you can consider both situation by this: 还是更好,因为值周围可能会有双引号,所以您可以通过以下两种方式考虑这两种情况:

sed -E 's/(ADDRESS_ORIGIN) *= *([\x27\x22])[^\039\034]*\2/\1 = \x27chapuza\x27/'

Where \\x27 = \\039 = ' , \\x22 = \\034 = " in RegEX. 在RegEX中\\x27 = \\039 = '\\x22 = \\034 = "

Like this: 像这样:

$ echo export const ADDRESS_ORIGIN = \"chupa-cabra.muz.id\"\;|sed -E 's/(ADDRESS_ORIGIN) *= *([\x27\x22])[^\039\034]*\2/\1 = \x27chapuza\x27/'
export const ADDRESS_ORIGIN = 'chapuza';

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

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