Ben*_*ell 135 linux terminal sed
我需要在配置文件的末尾添加以下行:
include "/configs/projectname.conf"
Run Code Online (Sandbox Code Playgroud)
到一个名为的文件 lighttpd.conf
我正在考虑使用sed这个,但我无法弄清楚如何.
如果该行尚不存在,我将如何仅插入它?
drA*_*erT 246
保持简单:)
grep + echo应该足够了:
grep -qxF 'include "/configs/projectname.conf"' foo.bar || echo 'include "/configs/projectname.conf"' >> foo.bar
Run Code Online (Sandbox Code Playgroud)
编辑:纳入@cerin和@thijs-wouters建议.
注意:
对于"编辑"改变我的答案:如果你有另一种解决方案给出你自己的答案!编辑旨在澄清,格式化,更好的英语等等!不要改变别人给出的答案!(谢谢)
rub*_*o77 69
这将是一个干净,可读和可重用的解决方案,使用grep和echo仅在文件尚不存在时添加一行:
LINE='include "/configs/projectname.conf"'
FILE='lighttpd.conf'
grep -qF -- "$LINE" "$FILE" || echo "$LINE" >> "$FILE"
Run Code Online (Sandbox Code Playgroud)
Pau*_*ce. 12
这是一个sed版本:
sed -e '\|include "/configs/projectname.conf"|h; ${x;s/incl//;{g;t};a\' -e 'include "/configs/projectname.conf"' -e '}' file
Run Code Online (Sandbox Code Playgroud)
如果您的字符串在变量中:
string='include "/configs/projectname.conf"'
sed -e "\|$string|h; \${x;s|$string||;{g;t};a\\" -e "$string" -e "}" file
Run Code Online (Sandbox Code Playgroud)
ham*_*x0r 10
如果写入受保护的文件,@ drAlberT和@rubo77的答案可能不适合你,因为一个人不能sudo >>.那么,一个类似的简单解决方案就是使用tee --append:
LINE='include "/configs/projectname.conf"'
FILE=lighttpd.conf
grep -qF "$LINE" "$FILE" || echo "$LINE" | sudo tee --append "$FILE"
Run Code Online (Sandbox Code Playgroud)
如果有一天,其他人必须将此代码作为"遗留代码"来处理,那么如果您编写一个不那么开放的代码,那么该人将不胜感激,例如
grep -q -F 'include "/configs/projectname.conf"' lighttpd.conf
if [ $? -ne 0 ]; then
echo 'include "/configs/projectname.conf"' >> lighttpd.conf
fi
Run Code Online (Sandbox Code Playgroud)
这是一个单行 sed,它执行内联工作。请注意,当变量存在时,它会保留变量的位置及其在文件中的缩进。这对于上下文通常很重要,例如当周围有注释或变量位于缩进块中时。任何基于“删除然后附加”范式的解决方案都在这方面失败了。
sed -i '/^[ \t]*option=/{h;s/=.*/=value/};${x;/^$/{s//option=value/;H};x}' test.conf
Run Code Online (Sandbox Code Playgroud)
使用一对通用变量/值,您可以这样编写:
var=c
val='12 34' # it handles spaces nicely btw
sed -i '/^[ \t]*'"$var"'=/{h;s/=.*/='"$val"'/};${x;/^$/{s//c='"$val"'/;H};x}' test.conf
Run Code Online (Sandbox Code Playgroud)
最后,如果您还想保留内联注释,可以使用 catch 组来实现。例如,如果test.conf包含以下内容:
a=123
# Here is "c":
c=999 # with its own comment and indent
b=234
d=567
Run Code Online (Sandbox Code Playgroud)
然后运行这个
var='c'
val='"yay"'
sed -i '/^[ \t]*'"$var"'=/{h;s/=[^#]*\(.*\)/='"$val"'\1/;s/'"$val"'#/'"$val"' #/};${x;/^$/{s//'"$var"'='"$val"'/;H};x}' test.conf
Run Code Online (Sandbox Code Playgroud)
产生:
a=123
# Here is "c":
c="yay" # with its own comment and indent
b=234
d=567
Run Code Online (Sandbox Code Playgroud)
另一个sed解决方案是始终将其附加在最后一行,并删除现有的。
sed -e '$a\' -e '<your-entry>' -e "/<your-entry-properly-escaped>/d"
Run Code Online (Sandbox Code Playgroud)
“正确转义”表示放置与您的条目匹配的正则表达式,即从实际条目中转义所有正则表达式控件,即在^ $ / *?+()前面加反斜杠。
这可能在文件的最后一行失败,或者如果没有悬挂的换行符,我不确定,但是可以通过一些漂亮的分支来解决...
| 归档时间: |
|
| 查看次数: |
76589 次 |
| 最近记录: |