code-8
code-8

Reputation: 58820

Edit to line with sed printing the new line twice

I have this settings in SSH configs (/etc/ssh/sshd_config)

AllowUsers root john

I want to add jane to the end of the line

AllowUsers root john jane 

I've tried

sed -i -e '/AllowUsers/{s/:/ /g;s/.*=//;s/$/ jane/p}' /etc/ssh/sshd_config && cat /etc/ssh/sshd_config

I kept getting this result

AllowUsers root john jane 
AllowUsers root john jane 

Why does extra line come?


Note

If I somehow run that command twice

I will get these result

AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 

run it again x3 times, will get me this

AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 

Upvotes: 0

Views: 172

Answers (1)

KamilCuk
KamilCuk

Reputation: 142080

The p flag in s/$/ jane/p will print the pattern space. Then pattern space will be printed again when the cycle is ended, resulting in two lines. Remove the p flag.

That said, just:

sed 's/AllowUsers .*/& jane/'

Upvotes: 5

Related Questions