sed.exe expression in bat file

5,398

I downloaded GNU Sed for Windows just to test this and I found out you don't need to escape anything for it to work inside a .bat file. Quotes " and their contents are interpreted correctly.

test.txt contents:

Test0 WRITE;
Test1 WRITE
Test2 WRITE;

test.bat contents:

sed -i "s/WRITE;/WRITE; TRUNCATE TABLE `Team_matchdayResults`;/g" test.txt

New contents of test.txt, after running test.bat:

Test0 WRITE; TRUNCATE TABLE `Team_matchdayResults`;
Test1 WRITE
Test2 WRITE; TRUNCATE TABLE `Team_matchdayResults`;

Did you try out Cygwin? It allows running and scripting common UNIX commands on Windows.

Share:
5,398

Related videos on Youtube

Jagger
Author by

Jagger

Updated on September 18, 2022

Comments

  • Jagger
    Jagger over 1 year

    I am using the Windows port of sed.exe.

    When I do something like that directly from the command line:

    sed.exe -i "s/WRITE;/WRITE; TRUNCATE TABLE `Team_matchdayResults`;/g" my_script.sql
    

    everything works flawlessly and the text parts are changed in the my_script.sql file.

    Now I want to do exactly the same but from within a .bat file. What I know is that I have to escape double quotes there, so I did it like that.

    sed.exe -i ""s/WRITE;/WRITE; TRUNCATE TABLE `Team_matchdayResults`;/g"" my_file.sql
    

    Unfortunately this ends with the following error

    sed.exe: -e expression #1, char 15: unterminated `s' command
    

    I suppose that I have to escape something more but I seem to have no idea what exactly. I have read somewhere that ; should be escaped in a batch file but only in FOR loop. However this has not brought me any further.

    What do I have to escape more?

  • Jagger
    Jagger almost 7 years
    You are right. I was escaping double quotes, because I did so in the past. Frankly said, no idea why... I know Cygwin, but in this case, I am not allowed to install it.