jmoneygram jmoneygram - 2 months ago 5
Bash Question

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. The script will be run within a .sh file, not typed into the console.

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. I just want to show where the tabs occur.

Any help is appreciated!!! Thanks!!!

Answer

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:]]

And \ instead of [[:space:]]