Reputation: 1637
I would like to diff two batches of files. If I simply put them in two different directories and diff by directory, the comparisons will be alphabetical which I do not want.
Another approach would be to list files in text1.txt and list files in text2.txt:
text1:
a1
b1
c1
text2:
c2
a2
b2
How can I approach this such that my loop will be:
diff a1 c2
diff b1 a2
diff b2 c1
Upvotes: 0
Views: 120
Reputation: 3838
Another solution :
#!/bin/bash
file1="..."
file2="..."
getSize(){
wc -l "$1"|cut -d " " -f1
}
getValueFromLineNumber(){
sed -n "$1p" "$2"
}
diffFromLineNumber(){
f1=$(getValueFromLineNumber "$1" "$file1")
f2=$(getValueFromLineNumber "$1" "$file2")
diff "$f1" "$f2"
}
# get min size
[[ $(getSize "$file1") -le $(getSize "$file2") ]] && min=$s1 || min=$s2
for (( i=1 ; i <= "$min" ; i++)); do
diffFromLineNumber "$i"
done
This solution takes care of the case where the two files don't have the same number of lines.
Upvotes: 0
Reputation: 241671
In bash, you can use the -u
flag on read to read from a different fd. This allows you to read from two files in parallel:
while read -r -u3 file1 && read -r -u4 file2; do
diff "$file1" "$file2"
done 3<file1.txt 4<file2.txt
Upvotes: 2
Reputation: 15746
You can use paste
to join the two files, then a bash loop to process.
paste text1 text2 | while read file1 file2; do diff "$file1" "$file2"; done
Upvotes: 2