0

i have file with complex text

    print("ERROR: passwords don't match")

password = hash_func(password.encode("UTF-8")).hexdigest()

i need to insert this text between them

with open('/etc/openvpn/clients/%s/login.txt' % username, 'w') as login_log:
    login_log.write('%s\n%s\n' % (username, password))

so it will be like this

 else:
        print("ERROR: passwords don't match")


with open('/etc/openvpn/clients/%s/login.txt' % username, 'w') as login_log:
    login_log.write('%s\n%s\n' % (username, password))

password = hash_func(password.encode("UTF-8")).hexdigest()
  • Is there a pattern which can be used? Is it always print("ERROR: passwords don't match") gollowed by password = hash_func(password.encode("UTF-8")).hexdigest()? – mnille Mar 16 '16 at 14:13
  • 1
    You Can edit the file with vi or nano, if you have Graphical interface then any text editor like gedit can be used. – AReddy Mar 16 '16 at 14:18

2 Answers2

0
cat > user.inp << EOL

with open('/etc/openvpn/clients/%s/login.txt' % username, 'w') as login_log:
    login_log.write('%s\n%s\n' % (username, password))
EOL
sed -i '/ERROR: passwords/r user.inp' /root/add.py
fi
0

You can do it with vi:

vi -c '/print("ERROR: passwords don'\''t match")/a\

with open('\''/etc/openvpn/clients/%s/login.txt'\'' % username, '\''w'\'') as login_log:
    login_log.write('\''%s\n%s\n'\'' % (username, password))
.' myfile

(This is all a single command.)

This will leave you with the file open in vi and the modification made, but the file not saved. You can look at it and see if it's the way you want.

To exit and save the changes, type :x and press enter. To exit and discard the changes, type :q! and press enter.

Any trouble, press <Esc> once or twice and then try :q! again.


The quoting on scripted edits can get atrocious very fast. I recommend you just learn to use vi and open the file to edit it interactively; it will be much easier than puzzling out the quoting in the above command.


Much, much easier, if you already have the lines included in one file, and you want to insert them in the other file at a specific place, then you could tweak some ex code I've already written to do just that.

Wildcard
  • 36,499