简体   繁体   English

如何在Linux Bash脚本文件中使用'sed'注释掉带有制表符间距的特定行?

[英]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.

相关问题 我如何使用sed -i命令替换从文件中特定行开始到另一行的字符出现? - How can i use sed -i command to replace the occurance of character starting from particular line in a file till another line? bash脚本中的Linux sed - Linux sed in bash script 如何将linux bash脚本文件添加到terraform代码中? - How can i add linux bash script file into terraform code? 在Linux中使用sed取消注释和注释一行配置文件 - Uncomment and comment a line of config file using sed in linux 无法使用linux sed在文件的特定行插入文本 - unable to insert text at a particular line of a file using linux sed Bash Linux 使用 sed 编辑一行并创建新行 - Bash Linux use sed to edit a line and create new lines 在Linux shell bash脚本中,如何在同一行打印到文件? - In Linux shell bash script, how to print to a file at the same line? 如何在使用 csv 脚本处理后删除 csv 文件中的一行 bash 脚本 - How to delete a line in a csv file after processing it with a Linux bash script 我可以在 Linux bash 脚本中使用什么命令将数值数据的可变行汇总为两个单独的变量,然后打印出来? - What command can I use in Linux bash script for summing up variable lines of numeric data into two separate variables that get printed out? 如何在Linux中使用sed将控制字符^ @插入文件中? - How can I insert control character ^@ into a file using sed in Linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM