1

I am looking for changing a file content which is repeated twice with that replacement I want to add extra line to second content in the same file

Example file

User YOURNAME
IdentityFile ~/.ssh/YOURKEY

.
.
.
User YOURNAME
Installing
Installing

Example Output after running script

User adminuser
IdentityFile ~/.ssh/id_rsa

.
.
.
User adminuser
IdentityFile ~/.ssh/id_rsa
Installing
Installing

I am able to change the user and YOURKEY with the below sed command

`sed- i s/"YOURNAME/adminuser"/g /root/.ssh/config`
`sed -i 's/YOURKEY/id_rsa/g' ff1`

But I'm unable to insert IdentityFile ~/.ssh/id_rsa in the next line.

edited

additional info ****User adminuser is having space at the beginning of the line. These files are getting synced on daily bases, So cant remove IdentityFile line. After sync it will get replaced

Final edit which works as required

perl -i -ne 'next if /IdentityFile/; 
            s#YOURNAME#adminuser\n    IdentityFile ~/.ssh/id_rsa#; 
            print' filename
  • additional info ****User adminuser is having space at the beaning of the line – Mongrel Jun 10 '16 at 10:01
1

Just remove all cases of IdentityFile and then add them again explicitly:

$ perl -i -ne 'next if /IdentityFile/; 
            s#YOURNAME#adminuser\nIdentityFile ~/.ssh/id_rsa#; 
            print' file
$ cat file
User adminuser
IdentityFile ~/.ssh/id_rsa

.
.
.
User adminuser
IdentityFile ~/.ssh/id_rsa
Installing
Installing

The next if /IdentityFile/ skips any lines matching IdentityFile. The s#YOURNAME#adminuser\nIdentityFile ~/.ssh/id_rsa# will replace any instance of YOURNAME with adminuser, a newline and the IdentityFile line. The final print prints all lines.

  • Thanks Terdon, I am getting the output as required, but its not saved and can we have ****space at the starting of the IdentityFile line. – Mongrel Jun 10 '16 at 11:25
  • @Mongrel just use -i. Perl is where sed got that idea from anyway. See updated answer.I have no idea what you mean with **** space. Are the * supposed to be * or spaces? What difference does that make? – terdon Jun 10 '16 at 11:27
  • @Mongrel you can use \t right after \n – Rahul Jun 10 '16 at 11:32
  • 1
    @Rahul why would I? Oh, you think that's what the OP means by "***** space`? If so, why assume they want a tab and not spaces? In any case, this really should be clarified in the question itself. Thanks for the edit, by the way. – terdon Jun 10 '16 at 11:33
  • \t will give me 8 space and I am looking for 4 space only. Rahul you are right I can user \t for <tab> – Mongrel Jun 10 '16 at 11:37
1

Part of the problem is that your template is inconsistent: the first occurrence has an IdentityFile line, while the second does not. You can make it consistent by first deleting the existing IdentityFile lines, and then by adding lines which you want.

To delete lines:

sed -i '/^IdentityFile /d' filename

To add lines, you can do this in sed by matching the User line, and appending a line, e.g.,

sed -e '/^User /'a'\
IdentityFile ~/.ssh/id_rsa' filename
  • Thomas I tried adding below the User section but in that file there is one more User section above. if there is a way which can verify if IdentityFile exist if not then add the line. – Mongrel Jun 10 '16 at 9:56
  • these files are getting synced on daily bases, So cant remove IdentityFile line. After sync it will get replaced. – Mongrel Jun 10 '16 at 10:28

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.