1

I would like to change all password (to be encrypted) in a file, with 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:

someCompanie:
    username: testUsername
    password: testPassword <--- password i want to be encrypted
someOtherCompanie:
    username: testOtherUsername
    password: testOtherPassword <--- password i want to be encrypted

the result wanted is like this:

someCompanie:
    username: testUsername
    password: a2e48043fcc30e2982ed43fbab01824f1efdaf8eddcc1ac914d1815d7a8738c4 <--- password encrypted
someOtherCompanie:
    username: testOtherUsername
    password: e7c3161f3655662da261015669c3ca449b4c4a738053088b363e83ba93ebdb8f <--- password encrypted

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

  • 1
    I'm not expert enough to give you the correct command, but you should probably be able to do this with only `sed`; no need for `grep`, `cut`, or `xargs`. – Moshe Katz Jun 19 '22 at 22:26
  • Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). – Cyrus Jun 20 '22 at 08:39

2 Answers2

2
$ cat tst.sh
#!/usr/bin/env bash

encrypt() {
    printf '%s' "$1" | md5sum | cut -d' ' -f1
}

while IFS= read -r line; do
    if [[ "$line" =~ ^([[:space:]]*password:[[:space:]]*)(.*) ]]; then
        line="${BASH_REMATCH[1]}$(encrypt "${BASH_REMATCH[2]}")"
    fi
    printf '%s\n' "$line"
done < "${@:--}"

$ ./tst.sh file
someCompanie:
    username: testUsername
    password: fed3b61b26081849378080b34e693d2e
someOtherCompanie:
    username: testOtherUsername
    password: 7c0453eb71acb77f386b2798767dc97a

Replace the contents of encrypt() with a call to whatever encryption tool you want to use, called however you want to call it.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
1

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
HatLess
  • 10,622
  • 5
  • 14
  • 32