Thank you to the guys at HEGE supporting Badcaps [ HEGE ] [ HEGE DEX Chart ]

Announcement

Collapse
No announcement yet.

Using sed to search and replace exact string.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Using sed to search and replace exact string.

    Hi!

    I am attempting to use sed to change a file. The contents are something like this:
    Code:
    #PasswordAuthentication yes
    PasswordAuthentication yes
    I want to change the second PasswordAuthentication to no. However, I am having some issues. My sed command is changing both to no.

    This is what I've tried
    Code:
    sed -i 's/\<PasswordAuthentication yes/PasswordAuthentication no/g' sshd_config
    That changes both.

    Code:
    sed -i "s/\bPasswordAuthentication yes/PasswordAuthentication no/g" sshd_config
    That changes both.

    Code:
    sed -i "s/\>PasswordAuthentication yes/PasswordAuthentication no/g" sshd_config
    That changes neither.

    I guess I'm getting a bit confused about word boundaries. Any ideas what I'm doing wrong and how to accomplish what I'm trying to accomplish?

    Thanks!

    **EDIT: I believe I found the solution.

    Code:
    sed -i "s/^PasswordAuthentication yes*$/PasswordAuthentication no/g" sshd_config
    that seems to change PasswordAuthentication and not #PasswordAuthentication.
    Last edited by Spork Schivago; 10-18-2018, 10:22 AM. Reason: Found the solution!
    -- Law of Expanding Memory: Applications Will Also Expand Until RAM Is Full
Working...
X