I would like to change all password in a file in a command and i'm stuck with my sed, i don't know how i can tell it to take the output of the sed
the example in my propeties file is like this:
username: testUsername
password: testPassword <--- passwords i want to be change
Here's what i got:
grep "password" application.yml | cut -d ":" -f2 | xargs -I pass sed -ie 's,'"$(pass)"',test,g' "app2"
and here's my output:
sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
Thanks very much for your attention,
Florian
CodePudding user response:
With GNU sed:
pass="new_password"
sed -i -E 's/(password: ).*/\1'"$pass"'/' application.yml
Output to application.yml:
username: testUsername password: new_password
The new password must not contain /
.
See: The Stack Overflow Regular Expressions FAQ
CodePudding user response:
Using sed
, match only lines that begin with password
to carry out the substitution on.
-i.bak
will create a backup of the file while saving the original file in place
$ sed -i.bak '/^password/s/ .*/ newPassword/' input_file
username: testUsername
password: newPassword