Saturday, November 24, 2012

Linux: sed command usage #1

I'm getting better with sed all the time but today was a challenging little booger.  I wanted to change /etc/pam.d/su from

cat /etc/pam.d/su
#%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
# Uncomment the following line to require a user to be in the "wheel" group.
#auth           required        pam_wheel.so use_uid
auth            include         system-auth
account         sufficient      pam_succeed_if.so uid = 0 use_uid quiet
account         include         system-auth
password        include         system-auth
session         include         system-auth
session         optional        pam_xauth.so

to

cat /etc/pam.d/su
#%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
# Uncomment the following line to require a user to be in the "wheel" group.
auth           required        pam_wheel.so use_uid
auth            include         system-auth
account         sufficient      pam_succeed_if.so uid = 0 use_uid quiet
account         include         system-auth
password        include         system-auth
session         include         system-auth
session         optional        pam_xauth.so

Notice the absense of the comment on line 6?  If you used sed -i 's/\#auth/auth/' <file> it would only look at the first line and then quit.  If you used sed -i 's/\#auth/auth/g' it'd change all of them.  I wanted to change just ONE out of the whole file.  The trick?  To specify a LINE for sed to work on.

sed -i '6s/\#auth/auth/' /etc/pam.d/su

Finally!  Looked for a while to find the answer.  The key is using the line number at the beginning of the search string here.  This link helped me get going in the right direction, http://www.linuxquestions.org/questions/linux-software-2/sed-display-text-on-specific-line-of-text-file-397405/. but it didn't give me EXACTLY what I wanted. However it helped me figure out how to specify a line.  I knew I could do it after I was able to print the line I wanted to work on.  SUCCESS!!

No comments:

Post a Comment