I need to compare two files and display the difference in table format using shell script. for example.


ep-devel- 2.23.2-23.rpm



output will be in:

Name                           |        file1              | file2
bp                             |         3.42.4            | 3.43.4
cp-devel                       |         3.23.2            | 4.33.2
dp                             |                           | 4.52.4
ep-devel                       |         2.23.2-23         | 
  • Is the filextension always .rpm. Consist the version number always of three blocks of numbers? – finswimmer Feb 12 at 8:22
  • yes the file extension is always .rpm . but the version number blocks will differ sometimes it will be 5 to 6 blocks of numbers. – Naik. Feb 12 at 8:25
  • This is not comparing files, which could be achieved by diff. This is more like comparing data in database from two different tables. Certainly doable in shell, but maybe that's not the right way to do it? – DevilaN Feb 12 at 8:27
  • i tried diff but i am not getting output what i want. is any other way to do this? – Naik. Feb 12 at 8:30
  • Do the package names contain exactly one hyphen, or can you have ap-dev.2.35.foo-bar.3.rpm ? – Mark Plotnick Feb 12 at 8:30
  1. cat file1| awk -F "-" '{print $2}'| sed "s/\.[a-z].*//g">file_1_final.txt
  2. cat file2| awk -F "-" '{print $2}'| sed "s/\.[a-z].*//g">file_2_final.txt
  3. paste file_1_final.txt file_2_final.txt | sed '1i file1 file2' >combined_file1_file2
  4. awk -F "-" 'NR==FNR{a[$1];next}($1 in a){print $1}' file1 file2>>common_difference_file_1_2
  5. awk -F "-" 'NR==FNR{a[$1];next}!($1 in a){print $1}' file1 file2>>common_difference_file_1_2
  6. sed -i '1i name' common_difference_file_1_2
  7. paste common_difference_file_1_2 combined_file1_file2


name    file1     file2
ap  2.21.3  2.35.3
bp  3.42.4  3.43.4
cp  3.23.2  4.33.2
dp      4.52.4
  • Hi @Praveen Kumar BS, Thanks for the solution. its working now with this example. what if i have the name and version like this ap-devel-2.21.3-4.4.65.rpm. here name is ap-devel and version is 2.21.3-4.4.65. – Naik. Feb 14 at 8:02
  • I think it works fine even in this case too test and let me know for any doubts – Praveen Kumar BS Feb 14 at 15:46
  • I tried In that case and its not working. – Naik. Feb 14 at 19:09

Based on your comments one can describe the filenames like this:

  • the program version consists of numbers, dots and hyphens and is preceded by a hyphen
  • the program names are all characters before that hyphen
  • the version number is delimited to the next block by a dot followed by a letter

With these information one can use sed to transform the names in a table per file containing only the program name and the version number:

$ sed -n 's|^\(.\+\)-\([0-9.-]\+\)\..*|\1 \2|p' file1.txt 
$ sed -n 's|^\(.\+\)-\([0-9.-]\+\)\..*|\1 \2|p' file2.txt 

One can then use awk to collect the program names in the two files, store which version of each program is in each file and print a summary table:

$ awk -v FS=" " -v OFS="\t" ' \
    { program[$1]++; } \
    NR==FNR { f1[$1] = $2; next; } \
    { f2[$1] = $2; } \
    END { for(i in program) print i, f1[i], f2[i]; }' \
   <(sed -n 's|^\(.\+\)-\([0-9.-]\+\)\..*|\1 \2|p' file1.txt) \
   <(sed -n 's|^\(.\+\)-\([0-9.-]\+\)\..*|\1 \2|p' file2.txt)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.