~ / archivo / stackoverflow / 44421118
How to write a dynamic string replacement in git bash?
-
This is on Windows 10 using the Linuxy environment provided with Git Bash.
I have this line in my target source file:
'version' => 'v1.1.023',I have this function in my
~/.bash_profile:function tag() { .... tagname="$1" cmd="s:v[0-9]\\.[0-9]\\.[0-9]+:$tagname:g;" perl -p -i -e "$cmd" config/app.php ... }When I run the command:
$ tag v.1.1.024The string is not replaced, therefore the version is not updated in my code. I tried a very similar script with sed instead of perl. The file isn't getting changed. I tried the same perl replacement syntax on CentOS and it worked. Note that the double-backslash appears to be required in order for the command to work on the next line when it's run in perl. That is to say, the variable string representation of the command has a double backslash so that it becomes a literal "." in the replacement.
Any ideas?
- ikegami 2017-06-07 19:35 Re "Note that the double-backslash appears to be required", Shouldn't be.
printf '%s\n%s\n' "\." "\\."shows that"\."and"\\."are functionally identical. - ikegami 2017-06-09 18:07 Please provide a minimal, runnable demonstration of the problem
- ikegami 2017-06-07 19:35 Re "Note that the double-backslash appears to be required", Shouldn't be.
-
-idoesn't work on Windows builds ofperl.>perl -i -pe1 foo Can't do inplace edit without backup.That feature uses anonymous files, which aren't supported by Windows. That said, you're not using a Windows build of
perlbut a cygwin build or similar. It could be that your unix emulation environment emulates anonymous files, so that might not be the the problem.But if it is the problem, replacing
-iwith-i.bakwill solve it. (Feel free to follow up withrm config/app.php.bak.)
By the way, you are generating Perl code, which is fragile. (Tag names containing
:,\,$or@will cause the code to fail.) I recommend one of the following instead:TAGNAME="$tagname" perl -i -pe's:v[0-9]\.[0-9]\.[0-9]+:$ENV{TAGNAME}:g' config/app.phpor
perl -i -pe' BEGIN { $tagname = shift(@ARGV); } s:v[0-9]\.[0-9]\.[0-9]+:$tagname:g; ' "$tagname" config/app.php- Sinan Ünür 2017-06-07 23:35 The OP is using a
perlbuilt with MinGW gcc:C:\> "\Program Files\git\usr\bin\perl.exe" -v:This is perl 5, version 22, subversion 1 (v5.22.1) built for x86_64-msys-thread-multi - ikegami 2017-06-08 04:43 @Sinan Ünür, Why are you telling me that? So I can test
-iwith it? I don't see how that helps me achieve that. Why didn't you do it? - phpguru 2017-06-09 17:48 I tried both, neither work for me. A .bak file is generated even though the target file is unchanged, I am using rm on the next line.
- ikegami 2017-06-09 18:06 Re "A .bak file is generated even though the target file is unchanged", That means the pattern doesn't match.
- phpguru 2017-06-09 18:15 Oh ok, I found the problem! Thanks for your help.
- phpguru 2017-06-09 18:23 I ended up with TAGNAME="$tagname" perl -i -pe's:v[0-9]\.[0-9]\.[0-9]+:$ENV{TAGNAME}:g' config/app.php and then on the next line rm config/app.php.bak
- Sinan Ünür 2017-06-07 23:35 The OP is using a
-
Hi @phpguru I would follow the KISS principle.
Using sed and avoiding perl
6.3. When should I use sed?
When you need a small, fast program to modify words, lines, or blocks of lines in a textfile.
function tag() { tagname="$1" sed -i -e "s:v[0-9]\.[0-9]\.[0-9]\+:$tagname:g" test.php }Output in my environment:
m.ortiz.montealegre@CPX-XYR3G1DTHBU MINGW64 ~ $ cat .bash_profile function tag() { tagname="$1" sed -i -e "s:v[0-9]\.[0-9]\.[0-9]\+:$tagname:g" test.php } m.ortiz.montealegre@CPX-XYR3G1DTHBU MINGW64 ~ $ cat test.php 'version' => 'v1.1.023, m.ortiz.montealegre@CPX-XYR3G1DTHBU MINGW64 ~ $ tag v1.1.024 m.ortiz.montealegre@CPX-XYR3G1DTHBU MINGW64 ~ $ cat test.php 'version' => 'v1.1.024,- ikegami 2017-06-08 04:46 Your code suffers from an injection bug. Using
sedcorrectly would be extremely complicated. KISS dictates you should avoidsed. - ikegami 2017-06-08 04:49 Even if you're ok with the injection bug,
sedis no simpler thatperlhere. It's literally the exact same program except for the extra\before the+needed bysed. - Miguel Ortiz yo 2017-06-08 11:01 Why would you use Perl which you already said is fragile? Sed has been around for many years and is the perfect tool to modify strings or lines of code sed.sourceforge.net/sedfaq6.html
- Miguel Ortiz yo 2017-06-08 12:32 @ikegami using sed may be complex but not in this case.
- ikegami 2017-06-08 16:28 I didn't say Perl is fragile; I said generating Perl code in a shell script is fragile. Generating sed code in a shell script like you did is equally fragile. In fact, your
sedcode-building suffers from exactly the same injection bug as the OP's Perl code building. Fix your injection bug if you think it's not complex; I think you'll find the Perl solution to be much simpler. - phpguru 2017-06-09 17:51 Tested on my environment - it doesn't work. No clue why. It's driving me nuts.
- ikegami 2017-06-08 04:46 Your code suffers from an injection bug. Using