I want to remove 1st character on file when grep some string. But need that changed row to be on the same position before the edit.
Example file:
#%PAM-1.0
auth sufficient pam_rootok.so
# Uncomment the following line to implicitly trust users in the "wheel" group.
#auth sufficient pam_wheel.so trust use_uid
auth include system-auth
account sufficient pam_succeed_if.so uid = 0 use_uid quiet
Expected View after the edit:
#%PAM-1.0
auth sufficient pam_rootok.so
# Uncomment the following line to implicitly trust users in the "wheel" group.
auth sufficient pam_wheel.so trust use_uid
auth include system-auth
account sufficient pam_succeed_if.so uid = 0 use_uid quiet
In that case on 4th row need just to remove “#”, but I want to do that when search the string “sufficient pam_wheel.so trust use_uid” not when point the exact row that I want to edit.
This is a job for
sed:Regexplanation:
So effectively we are matching lines starting with
#containing the stringsufficient\s+pam_wheel.so trust use_uidand removing the#Notes: the
-rflag is for extended regexp, it might be-Efor your version ofsedso check theman.If you want to store the changes back to the
fileuse the-ioption:If the column aligment is important then capture up to
sufficientand after it so you get 2 capture groups and replace with\1 \2.Edit:
Replace the string
AllowUsers support adminwith#AllowUsers support admin: