I have about 1000 html files in a directory that are different versions of a webpage. Each is named by the date the page was modified - e.g. 20190104, 20190105 - but there are occasional gaps, such as between 20190105 and 20190107.
I know how to use diff to compare one file to the immediately subsequent version and save the differences to a third file, but I want a loop that will that for all 1000. The standard "for i in ./*" doesn't work because I need to reference multiple files inside the loop.
I suspect I need to turn the list of files into an array, and then have a for loop that references array elements n and n 1, but I'm having trouble with the syntax.
CodePudding user response:
elements n and n 1
No, don't think in the future - you don't know it. You know the past - iterate over n-1 and n.
prev= # empty previous filename
for cur in ./*; do
if [[ -n "$prev" ]]; then # if prev is Nonempty
do_stuff "$prev" "$cur"
fi
prev=$cur
done
CodePudding user response:
No need for an array
last=BEGIN
for f in $(echo * | sort); do
echo diff "$last" "$f"
diff "$last" "$f"
last="$f"
done
CodePudding user response:
Comparing by pairs:
#!/usr/bin/env bash
comparePairs() {
while [ "$2" ]; do
printf 'Compare %s with %s.\n' "$1" "$2"
shift
done
}
comparePairs foo bar baz cuux
output:
Compare foo with bar.
Compare bar with baz.
Compare baz with cuux.
Now your diff pairs:
#!/usr/bin/env bash
diffPairs() {
while [ "$2" ]; do
diff "$1" "$2" > "$1-$2.diff"
shift
done
}
diffPairs "$@"
