Comparing list of files in parallel

Hi everyone.

I have a list of files like:

file001
file002
file003
.
.
.
.
file385
file386
file387

There are more files than above, but I hope you understand what I'm trying to do here.

Is there a way I can create a loop to compare:
file001 with file385
file002 with file386
file003 with file387

I tried a nested loop, but all it did was compare file001 with file385, file386, file387.
Then is compared file002 with file385, file386, file387,,,etc.

I just want two sets of ranges compared in parallel

Thanks in advance.

There are lots of ways... What operating system and shell are you using?

bash

I've tried the followin:

a=({$bfile..$efile})
b=({$bsfile..$esfile}

for (( c=0 ; c<= ${#a[@]} ; c++ ))
  do
    echo "${a[c]}" "${b[c]}"
  done

)[/CODE]

Perhaps something more like:

#!/bin/ksh
base="file"
start1=1
start2=385
for ((i = start1; i < start2; i++))
do	f1="$(printf '%s%03d' "$base" $i)"
	f2="$(printf '%s%03d' "$base" $((i + start2 - start1)))"
	if [[ -f "$f1" ]] && [[ -f "$f2" ]]
	then	printf '%s\n' "*** comparing $f1 to $f2"
		cmp "$f1" "$f2"
	else	printf '%s\n' "*** $f1 or $f2 is not a regular file; exiting"
		break
	fi
done

will do what you want. This was written an tested using a 1993 version of the Korn shell, but also works with bash .

1 Like

Still working on it Don. Getting close.
Thanks much for your reply!

Just for the fun of it - and the logics - try

ls -r file*| for j in file*; do  read i; if [ $i == $j ]; then break; fi; echo $i"  "$j; done
file387  file001
file386  file002
file385  file003
file384  file004

I am having a problem I think is similar to this one. I recently started a new class and I am way behind and I do not have a clue. I got one class with powershell ISE which that one I have ok I can get the work but for this shell and scripting class I am lost. I found a script but it only opens one file so I don't know what to do with that. I DL Fedora virtual OS on USB and I am running that then i used the terminal to install kornshell:) I ran this code I found and changed the name to testfile 1 and test file2 but I can only get it to read one file.

I used this code that I found here
#!/bin/ksh
file="/path/to/file.txt"
# while loop
while IFS= read -r line
do
# display line or do somthing on $line
echo "$line"
done <"$file"

and I tried to change it by adding
echo "$line" twice and changing
(while IFS= read -r line)
to

while IFS= read -r line1
IFS= read -r line2

but it does not work anymore. I will add the assignment details as well

I have never done this before but I think I can learn it, I think it has something to do with echo and if I can add both files to that part, I read something about arrays too but I am not sure that will apply here. If I take off line and add the file name instead will that change the outcome?

Moderator comments were removed during original forum migration.
num_of_files=`ls leo| wc -l`
loop_size=$((${num_of_files}/2))
i=1
while [ $i -le $loop_size ];do
  let tmp=$i+$loop_size
  echo "comparing file_${i} with file_${tmp}"
  let i=$i+1
done

root@qas0009[23:08:25]:/tmp>ls leo

file_1  file_10  file_11  file_12  file_13  file_14  file_15  file_16  file_17  file_18  file_19  file_2  file_20  file_3  file_4  file_5  file_6  file_7  file_8  file_9

output

comparing file_1 with file_11
comparing file_2 with file_12
comparing file_3 with file_13
comparing file_4 with file_14
comparing file_5 with file_15
comparing file_6 with file_16
comparing file_7 with file_17
comparing file_8 with file_18
comparing file_9 with file_19
comparing file_10 with file_20