bash 使用 sed 编辑 /etc/fstab

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19147776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:09:03  来源:igfitidea点击:

using sed to edit /etc/fstab

bashsed

提问by Brian Kuepper

/etc/fstab line I want to edit:

/etc/fstab 行我想编辑:

/dev/appvg/home         /home     ext3    defaults        0 0

How do I get sed to insert ",nodev" behind the defaults column?

如何让 sed 在默认列后面插入“,nodev”?

I found this command in another post that inserts it before "defaults", and I can't figure out how get to insert after:

我在另一篇文章中发现了这个命令,该命令在“默认值”之前插入它,但我不知道如何在之后插入:

sed -ie 's:\(.*\)\(\s/home\s\s*\)\(\w*\s*\)\(\w*\s*\)\(.*\):nodev,:' /etc/fstab

which gives me this:

这给了我这个:

/dev/appvg/home         /home     ext3    nodev,defaults  0 0

This is how I want it to look like:

这就是我想要的样子:

/dev/appvg/home         /home     ext3    defaults,nodev  0 0

Is it possible? I'm new to sed...

是否可以?我是 sed 的新手...

回答by Paul Wagland

The simplest answer to your question, assuming that this is exactly what you want to change is:

假设这正是您想要更改的,对您的问题的最简单答案是:

sed -ie '/^\/dev\/appvg\/home/ s/defaults/defaults,nodev/' /etc/fstab

What this says to do is to match the line starting (^) with ^/dev/appvg/home, and then apply the following command to it. In this case, the following command is "change defaults to defaults,nodev"

这表示要做的是将以 (^) 开头的行与 ^/dev/appvg/home 匹配,然后对其应用以下命令。在这种情况下,以下命令是“将默认值更改为默认值,nodev”

If you wanted to modify the command that you proposed to do the change, then you would want:

如果您想修改您建议进行更改的命令,那么您需要:

sed -ie 's:\(.*\)\s\(/home\)\s\s*\(\w*\)\s*\(\w*\)\s*\(.*\):   ,nodev :' /etc/fstab

The \1, \2, etc between the last two ':' characters are replacement markers, and they say to replace the contents of the matching brackets from the first part of the expression. By changing where the \s are in the matching expression, then you only get the word (\w) characters and not the space (\s) characters in your substitution. Because of this, you need to insert your own spaces into the resulting string.

最后两个 ':' 字符之间的 \1、\2 等是替换标记,它们表示替换表达式第一部分中匹配括号的内容。通过更改匹配表达式中 \s 的位置,您在替换中只会得到单词 (\w) 字符而不是空格 (\s) 字符。因此,您需要将自己的空格插入到结果字符串中。

回答by iamauser

You can also use awk. Generally in Linux, defaultscomes in the 4th column of the /etc/fstabfile. You could use that to print only if the line has a defaultin it.

您也可以使用awk. 通常在 Linux 中,defaults位于/etc/fstab文件的第 4 列。仅当该行中有 a 时default,您才能使用它进行打印。

awk '/defaults/{="defaults,nodev"}{print}' /etc/fstab

To be specific to your case,

具体到您的情况,

awk '/\/dev\/appvg\/home/{="defaults,nodev"}{print}' /etc/fstab

回答by Abu Shumon

You can try this way also:

您也可以尝试这种方式:

sed "s*/opt/apps/app1               ext4    defaults        1 1*/opt            ext4    defaults        1 2*g" -i /etc/fstab
  • from frist *to second *: string to be searched for
  • from second *to third *: string to be replaced with.
  • 从第一个*到第二个*:要搜索的字符串
  • 从第二*到第三*:要替换的字符串。

you can first try without -ioption which outputs changes on terminal but doesn't actually change in the actual file.

您可以先尝试不使用-i在终端上输出更改但实际上在实际文件中没有更改的选项。