0

I have tried this on Debian, but it fails:

echo "--db_host db --db_password $£é5+dd/gcç# --db_name zorgl" | sed -e "s/^.*db_password *\([^ ]+\) +.*$/\1/"

Result (no match):

--db_host db --db_password $£é5+dd/gcç# --db_name zorgl

Expected result:

$£é5+dd/gcç#
lalebarde
  • 1,684
  • 1
  • 21
  • 36

2 Answers2

1

Using sed

$ sed -E 's/.*db_password ([^ ]*).*/\1/' input_file
$£é5+dd/gcç#
HatLess
  • 10,622
  • 5
  • 14
  • 32
  • It fails for me with "invalid byte sequence" which switching to `-E` does nothing to fix. – tripleee Sep 27 '22 at 18:16
  • @tripleee Are you on Mac? Using GNU sed should work – HatLess Sep 27 '22 at 18:28
  • This was on a Mac, yes. The OP doesn't reveal how their `sed` is failing so the fact remains that `-E` is unlikely to fix their problem. – tripleee Sep 27 '22 at 18:30
  • Hmm. I see your point @tripleee. Guess we would need to wait for some form of elaboration from OP – HatLess Sep 27 '22 at 18:38
  • @HatLess' solution works for me. From the man page: "-E, -r, --regexp-extended, use extended regular expressions in the script (for portability use POSIX -E)." – lalebarde Sep 27 '22 at 20:03
0

Post by HatLess will fail if password contains space. This should do:

sed -E 's/.*db_password (.*?) --db_name.*/\1/' file
$£é5+d d/gcç#
Jotne
  • 40,548
  • 12
  • 51
  • 55
  • 2
    I think sed does not recognize the non greedy quantifier https://stackoverflow.com/questions/1103149/non-greedy-reluctant-regex-matching-in-sed – The fourth bird Sep 28 '22 at 08:06