Howto find changes in a script text using the UNIX diff command?

Hi all

I wrote a script that worked fine with release version 0.4.8. Then I made some changes into release 0.4.9. Now as I tried to use “diff” from the Terminal in order to show me the differences between both scripts (saved as text) it did not work as expected.

Did anyone experience how to let write the changed or added lines only into a resulting text file? Tried it that way:

Might it be, “diff” cannot deal with AppleScript but C-code only?

Any help is pretty much appreciated.

Best regards,
Thomas

The diff command should work on any file that has line breaks that use linefeed characters (normally UNIX style line breaks (LF only); DOS style line breaks (CR LF) also usually work since they have an LF in them, it just makes diff think that each line also has a CR at the end). Both files should use the same line ending convention (either UNIX or DOS), otherwise diff will think that every line is different (since, in its view, all the lines in one file will have trailing CRs and none of the lines in the other will have them).

Maybe your text files have Mac style line breaks (CR only (no LF!); which is an option in Script Editor when saving a script as text). Either save them back out as plain text making sure to select UNIX (LF) line endings (may not be possible in some editors/environments?), or use something like tr to convert the files from the command line.

tr \\r \\n < input-file > output-file

Once you have LF-based versions of the files, diff should work.

Edit: typo

Thank you chrys. Worked perfectly that way


and removes the temporary files. If now someone knows how to replace FILENAME with a var, it could be a pretty nice function to be placed as an alias in .profile or .bashrc for usage from within the terminal.

:slight_smile:
Thomas

In bash, aliases can not take individual parameters, so it will have to be a shell function:

[code]# Place this in a shell init file or in any random file so you use “source” or “.” to bring it into an individual shell’s environment.

Usage: diff-mac spartacus048.txt spartacus049.txt

diff-mac() {
local A=“$1” Atmp=“${1%.txt}.tmp” B=“$2” Btmp=“${2%.txt}.tmp”

# candidates for more parameters or fancy handling:
local DIFFOPTS="-a"
local OUTPUTFILE="spartacus.diffs.txt"

tr \\r \\n < "$A" > "$Atmp" \
	&& tr \\r \\n < "$B" > "$Btmp" \
	&& diff "$DIFFOPTS" "$Btmp" "$Atmp" > "$OUTPUTFILE"
open "$OUTPUTFILE" \
	&& rm "$Atmp" "$Btmp"

}[/code]
I coalesced the rm commands and changed your background command separator (single ampersand) between the diff and the open to a normal line break (unconditional, foreground, just like a semicolon). Running the diff in the background creates a race condition between populating the output file and the application associated with “.txt” files reading the contents. For a short diff output, you may never notice it, but for long diff output, the “.txt” application (e.g. TextEdit) might read the file before diff has finished writing it all out. This would probably manifest as truncated output, which you may or may not be readily identifiable.