I'm writing a script which should copy a client system and change some lines in a file.
There is a source file which has a line like:
$DB_HOST = "LegitDbHost";
And the destination file has a line like:
$DB_HOST = "testDbHost";
So i need to replace the destination line with the source line and I want to print out something like this:
Replacing line 12: '$DB_HOST = "testDbHost";' ==with==> '$DB_HOST = "LegitDbHost";'(y/n):
So far i got this:
toReplace='$DB_HOST';
sourceLine=$(cat ./sourceFile.php | grep -m 1 "$toReplace[[:space:],=]");
destLineNr=$(cat ./destFile.php | grep -n -m 1 "$toReplace[[:space:],=]" | grep -Eo '^[^:]+');
destLine=$(cat ./destFile.php | grep -m 1 "$toReplace[[:space:],=]");
read -p "Replacing line $destLineNr: $destLine ==with==> $sourceLine.(y/n): ";
however, my output looks like this:
==with==> $DB_HOST ST = "testDbHost";.(y/n):
I think it's because the sourceLine has a ;
inside and that gets interpreted as a command end.
I just don't know how to get around that.
I hope it's understandable.
Edit:
as suggested i tried using echo -n
toReplace='$DB_HOST';
sourceLine=$(cat ./sourceFile.php | grep -m 1 "$toReplace[[:space:],=]");
destLineNr=$(cat ./destFile.php | grep -n -m 1 "$toReplace[[:space:],=]" | grep -Eo '^[^:]+');
destLine=$(cat ./destFile.php | grep -m 1 "$toReplace[[:space:],=]");
echo -n "Replacing line $destLineNr: "
echo -n $destLine
echo -n " ===with===> "
echo -n $sourceLine
this prints out:
===with===> $DB_HOST = "testDbHost";OS";
dos2unix
on them first. – Stéphane Chazelas Feb 20 '21 at 10:11-n
flag to prevent a terminal newline being appended) and then callread -p '(y/n)?'
as the next command in the script? That should accomplish what you desire, if I'm understanding it correctly. – Peter J. Mello Feb 20 '21 at 10:59hd
orod
to just dump a specific line and show output. eg.grep DB_HOST sourceFile.php|hd
repeat with destFile.php if you haven't gothd
useod -cbx --endian big
(btw, your code works for me using bash) – X Tian Feb 20 '21 at 12:31