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 Answers 2

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
0
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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .