[英]How can I use 'sed' in a Linux Bash script file to comment out a particular line with tabbed spacing?
I wish to add '#' to the beginning of this line within a particular file using sed. 我希望使用sed在特定文件中此行的开头添加“#”。 The script will be run within a .sh file, not typed into the console.
该脚本将在.sh文件中运行,而不是在控制台中键入。
auth [success=1 default=ignore] pam_unix.so nullok_secure
Should be: 应该:
#auth [success=1 default=ignore] pam_unix.so nullok_secure
I'm currently trying commands similar to: 我目前正在尝试类似于以下命令:
sudo sed -e '/auth[[:tab:]][success=1[[:space:]]default=ignore][[:tab:]]pam_unix.so[[:space:]]nullok_secure/ s/^#*/#/' -i /etc/pam.d/common-auth
I know the above command is wrong, as [[:tab:]]
is not a valid command. 我知道上述命令是错误的,因为
[[:tab:]]
不是有效的命令。 I just want to show where the tabs occur. 我只想显示选项卡出现的位置。
Any help is appreciated!!! 任何帮助表示赞赏!!! Thanks!!!
谢谢!!!
On Ubuntu you can use \\t
for tab, and I believe that's even specified in POSIX. 在Ubuntu上,您可以使用
\\t
来制表符,我相信POSIX中甚至指定了该选项。
/auth\t\[success=1 default=ignore\]\tpam_unix.so nullok_secure/
I think I've run into issues doing that on BSD though, but if you don't really care that it's exactly a tab, and would accept the line even if it has other types of spaces you could be a little more flexible 我想我在BSD上遇到了这样的问题,但是如果您真的不在乎它确实是一个制表符,并且即使该行具有其他类型的空格也可以接受该行,那么您可能会更灵活一些
/auth[[:space:]]\+\[success=1 default=ignore\]pam_unix.so[[:space:]]nullok_secure/
which is a GNU-ism for the \\+
to mean one or more of the previous, to be more POSIX-y you could do it 这是一种GNU主义,表示
\\+
代表以前的一个或多个,成为POSIX-y,您可以做到这一点
/auth[[:space:]][[:space:]]*\[success=1 default=ignore\]pam_unix.so[[:space:]]nullok_secure/
(And note that the [:space:]
character class does not contain only the space character, it's the set of white space characters, which includes tabs.) (请注意,
[:space:]
字符类不只包含空格字符,它是一组空白字符,其中包括制表符。)
Also note, we have to escape the [
in the pattern you're matching, or it will define a character class of things to match, which is certainly not what you're trying for here. 还要注意,我们必须转义您要匹配的模式中的
[
,否则它将定义要匹配的事物的字符类,这肯定不是您要在此处尝试的。
As another option, assuming your common-auth
looks like mine, that's the only line that has success=1
on it so you could just use that, although it's more fragile if other people make changes to the file some time. 另一种选择是,假设您的
common-auth
看起来像我的,那是唯一一行上具有success=1
行,因此您可以使用它,尽管如果其他人在某个时间更改文件会更脆弱。
It works... 有用...
sed -e '/^auth\t\[success=1\ default=ignore\]\tpam_unix.so\ nullok_secure/ s/^/#/' -i file
I'm just using \\t
instead of [[:tab:]]
我只是使用
\\t
而不是[[:tab:]]
And \\
instead of [[:space:]]
和
\\
代替[[:space:]]
You're making this very hard on yourself. 您正在为此加倍努力。 The authconfig files are standard and won't need all of the regex you're using.
authconfig文件是标准的,不需要您使用的所有正则表达式。
sed -i 's/^auth [success=1 default=ignore] pam_unix.so nullok_secure/#auth [success=1 default=ignore] pam_unix.so nullok_secure/' /etc/pam.d/common-auth
If you really want to be specific though, I wouldn't use the space and tab lookups. 但是,如果您真的想具体一点,我不会使用空格和制表符查找。 Just look for the text you want and the white space in between.
只需查找所需的文本以及之间的空白。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.