masb
masb

Reputation: 2843

Using diff on multiple files with variable names

I have a bunch of files, ex:

error1.sol
error2.sol
comment1.sol

and so on

My program has created these output files:

myerror1.sol
myerror2.sol
mycomment1.sol

and so on

How can I in an elegant manner use diff to match

diff error1.sol myerror1.sol
diff error2.sol myerror2.sol

automatically?

Upvotes: 0

Views: 43

Answers (1)

John1024
John1024

Reputation: 113994

for fname in my*.sol
do
    diff "${fname#my}" "$fname"
done

The above looks for every file created by your program (my*.sol). Each such file is assigned, in turn, to the variable name fname. Using fname, we can get the name of the source file by removing my from the front. This is done with ${fname#my}. diff is then run on these two files. The loop repeats for as many such files as there are in the current directory.

You may find the output easier to read if an echo command is added to show which files are being compared:

for fname in my*.sol
do
    echo Comparing "${fname#my}" and "$fname"
    diff "${fname#my}" "$fname"
done

Upvotes: 2

Related Questions