简体   繁体   中英

Windows equivalent of the unix diff command with ed output

I'm attempting to recursively compare two directories that are structurally equivalent (XML documents in a decompressed excel file), and if any files differ, then overwrite the text from the first file with the text from the corresponding file in the second directory. In the Unix command line, you can easily output a script that will do this for two files using:

diff -e file1.xml file2.xml > edscript.txt

I can then invoke the script with:

echo "w" >> edscript.txt
ed - file1.txt < edscript.txt

However, I'm using Git Bash Windows 7, and there is no "ed" program installed. Is there an equivalent that can output a script and execute? If not, is there a way to install ed on git bash? I've already tried sudo apt-get install ed but the commands don't seem to be possible. My ultimate goal is to run this scripts using python with the subprocess library

Any advice or suggestions are welcome. Thanks!

是的,这些工具可用于Windows,请考虑使用CygwinMSyS

If you have git bash installed already, check in its bin folder. There should be a diff.exe that you can call directly.

For me, that's C:\\Program Files (x86)\\Git\\bin\\diff.exe

(This is probably easier than trying to call a *nix-like shell from python on windows, and then run diff on that command line)

Edit: Doing this from python, assuming the aforementioned diff.exe path:

In [40]: import subprocess
In [41]: subprocess.call(["c:/program files (x86)/git/bin/diff.exe", "-e", "c:/users/a.p/empty.py", "c:/users/a.p/.bashrc"])
alias l='ls -a'
alias ll='ls -la'
alias ipy='ipython'
.
Out[41]: 1

The GNU tools http://gnuwin32.sourceforge.net/packages/diffutils.htm

Also from cmd.exe the comp command, try help comp for parameters.

Edit: you have the python tag on this question, do you have python available? If so then look at the filecmp module in the standard library: https://docs.python.org/3/library/filecmp.html

Examples here: http://pymotw.com/2/filecmp/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM