2

I want to insert a line after matching line in sshd file which is present inside pam.d directory.

For e.g. it should search for this line

account    required     pam_nologin.so

and it should insert a line

account    required     pam_access.so

just below it, with proper line spacing between words.

I'm new to sed and awk, so any help would be appreciated.

2

This should work (reading from stdin, printing to stdout for testin purposes):

sed -e '/^account    required     pam_nologin\.so$/a account    required     pam_access.so'

the a command will append the text to the matched line, the matched line being one consisting of exactly "account required pam_nologin.so".

If you want to use the -i switch (edit a file in-place), I strongly suggest using -i .bak or similar to keep a backup of the original file:

sed -i .bak -e '/^account    required     pam_nologin\.so$/a account    required     pam_access.so' /etc/pam.d/sshd
1

Using good old ed:

ed /etc/pam.d/sshd << EOT
/account    required     pam_nologin.so/
a
account    required     pam_access.so
.
w
EOT

The /account required pam_nologin.so/ addresses a line.

The a command is for appending text to the editor buffer.

The text account required pam_access.so is entered into the buffer.

. returns to command mode.

w write out buffer to file.

  • I needed to use sed merely, though I really appreciate your help, will try it somewhere. Thanks. – Rahul Apr 10 '15 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.