简体   繁体   English

sed -e 's|PATH="\(.*\)"|PATH="/opt/man/common/bin:\1"|g' -i /etc/environment 行是什么意思

[英]what is meaning of sed -e 's|PATH="\(.*\)"|PATH="/opt/man/common/bin:\1"|g' -i /etc/environment line

以下命令是什么意思?

sed -e 's|PATH="\(.*\)"|PATH="/opt/man/common/bin:\1"|g' -i /etc/environment

It substitutes all the instances of它替换了所有的实例

PATH="somestring"

with

PATH="/opt/man/common/bin:somestring"

in the file /etc/environment在文件/etc/environment

In detail详细地

  • s|string1|string2| substitutes string1 with string2string2替换string1
    • the delimiter (in this case | ) is defined by the character following the substitute command ( s ).分隔符(在本例中为| )由替换命令 ( s ) 后面的字符定义。 More common is / .更常见的是/
    • the flag g at the end tells sed to substitute all non-overlapping matches (and not only the first one)最后的标志g告诉sed替换所有不重叠的匹配项(而不仅仅是第一个匹配项)
    • the \( and \) define a group (in this case everything between the quotes) \(\)定义一个组(在这种情况下,引号之间的所有内容)
    • the \1 is a back-reference to the first group \1是对第一组的反向引用
  • -i is telling sed to apply the changes directly in the file (inline) instead of writing to standard output. -i告诉sed直接在文件中应用更改(内联),而不是写入标准输出。

Edit编辑

As pointed out in the comments this regular expression is very fragile.正如评论中指出的那样,这个正则表达式非常脆弱。

  • It will match the string anywhere (not only after a blank or the beginning of a line).它将在任何地方匹配字符串(不仅在空格或行首之后)。 It will for example match CDPATH="string"例如,它将匹配CDPATH="string"
  • It will not match strings delimited by ' .它不会匹配由'分隔的字符串。 It will not match PATH='somepath' .它不会匹配PATH='somepath'
  • It will match strings in comments.它将匹配注释中的字符串。 For example # some comment with PATH="" in the text例如# some comment with PATH="" in the text
  • It will match inside other strings, for example STRING='PATH="somepath"它将匹配其他字符串,例如STRING='PATH="somepath"

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

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