简体   繁体   English

sed用新的目录结构替换“ Linux目录”行

[英]Sed replace line “Linux Directory ” with new Directory structure

So not sure, but i tried the following and it didn't work. 所以不确定,但是我尝试了以下操作,但没有成功。
What string can i use to replace a directory string in sed ? 我可以使用什么字符串替换sed的目录字符串?

sed 's"/usr/lib64/$id""/home/user1/$id"/g' 1.php > 1_new.php

Because your strings have slashes in them, it is convenient to use a different separator in the substitution expression. 因为您的字符串中包含斜杠,所以在替换表达式中使用其他分隔符很方便。 Here, I use | 在这里,我用| as the separator. 作为分隔符。 If, for simplicity, we ignore the double-quotes inside your regular expression, we could use: 如果为简单起见,如果我们忽略了正则表达式中的双引号,则可以使用:

sed "s|/usr/lib64/$id|/home/user1/$id|g" 1.php > 1_new.php

If we include the double-quotes, then quoting because a bit more complex: 如果我们将双引号包括在内,则进行引号是因为更为复杂:

sed 's|"/usr/lib64/'"$id"'"|"/home/user1/'"$id"'"|g' 1.php > 1_new.php

I assume that you intend for $id to be replaced by the value of the shell variable id . 我假设您打算将$id替换为shell变量id的值。 If so, it is important in both cases that the expression $id not be inside single-quotes as they inhibit variable expansion. 如果是这样,则在两种情况下重要的是表达式$id不能位于单引号内,因为它们会抑制变量扩展。

Discussion of Quoting 报价讨论

The quoting in the second case may look ugly. 第二种情况下的报价看起来很丑。 To help explain, I will add some spaces to separate the sections of the string: 为了帮助说明,我将添加一些空格来分隔字符串的各部分:

sed 's|"/usr/lib64/' "$id" '"|"/home/user1/' "$id" '"|g' # Don't use this form.

From the above, we can see that the sed command is made up of a single-quoted string, 's|"/usr/lib64/' followed by a double-quoted string, "$id" , followed by a single-quoted string, '"|"/home/user1/' , followed by a double-quoted string, "$id" , followed by a single-quoted string, '"|g' . 从上面可以看到, sed命令由单引号字符串's|"/usr/lib64/'和双引号字符串"$id" ,后跟单引号字符串'"|"/home/user1/' ,然后是双引号字符串"$id" ,然后是单引号字符串'"|g' Because everything is in single-quotes except that which we explicitly want the shell to expand, this is generally the approach that is safest against surprises. 因为除了我们明确希望外壳扩展的内容以外,所有内容都用单引号引起来,所以通常这是最安全的避免意外的方法。

Alternate Quoting Style 替代报价方式

The following quoting style may seem simpler: 以下引用样式似乎更简单:

sed "s|\"/usr/lib64/$id\"|\"/home/user1/$id\"|g" 1.php > 1_new.php

In the above, the sed command is all one double-quoted string. 在上面, sed命令是所有一个双引号字符串。 Double-quotes can be included within double-quoted strings by escaping them as \\" . This style is fine as long as you are confident that you know that the shell won't expand anything except what you want expanded. 可以通过将双引号转义为\\"来将双引号包含在双引号字符串中。只要确信您知道shell不会扩展任何内容(只要您要扩展的内容),此样式就可以了。

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

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