2

I'm very new to shell scripting and learning it now.

I have two files.

File 1:

1   StartInstall, CDM_2.5B263, OK       
2   EndInstall, CDM_2.5B263, SUCCESS    
3   StartPatch, CDM_2.5.0.2B1, OK       
4   StartPatch, CDM_2.5.0.3B1, OK       
5   EndPatch, CDM_2.5.0.3B1, SUCCESS 

File 2:

1   StartInstall, CDM_2.5B263, OK       
2   EndInstall, CDM_2.5B263, SUCCESS    
4   StartPatch, CDM_2.5.0.3B1, OK       
5   EndPatch, CDM_2.5.0.3B1, SUCCESS    

I should find the difference in file 2 compare to file 1. If any line missing, shell script should indicate the the missed line and write it in another text file (ex result.txt). Shell script should also indicate me that the missed line lies between which two lines.

For example the output should be like in Error.txt Line missing:

3   StartPatch, CDM_2.5.0.2B1, OK

Between 2 and 4

2

Using comm, since the files are sorted:

comm -3 file1 file2 > result.txt

The output contains the lines that are present in file1 but not file2, and also the lines that are present in file2 but not file1 prefixed with a tab.

If the files aren't sorted, sort them first. Assuming that you're using bash, ksh or zsh and not plain sh:

comm -3 <(sort file1) <(sort file2) > result.txt
  • Thanks, but comm is used for a sorted file , how do i sort these files ? – Parul kusmatia Apr 1 '17 at 14:20
  • @Parulkusmatia The files in your question are sorted. If the real files aren't, run sort first. – Gilles 'SO- stop being evil' Apr 1 '17 at 14:21
  • done that , but it is not giving me the output i need – Parul kusmatia Apr 1 '17 at 14:23
  • Can you please tell me what to do when i have multiple enteries missing in file 2 and some extra enteries, what can be used then ? @Gilles – Parul kusmatia Apr 1 '17 at 14:32
  • Can you please tell me what to do when i have multiple enteries missing in file 2 and some extra enteries, what can be used then ? @phk – Parul kusmatia Apr 1 '17 at 14:33
1

This is probably the easiest way. Notice < is an omission and | indicates a character in the line is different. (Line 5 in file1.txt had some whitespace characters missing in your copy-paste).

$ diff -y file1.txt file2.txt > diff.txt

1   StartInstall, CDM_2.5B263, OK                               1   StartInstall, CDM_2.5B263, OK
2   EndInstall, CDM_2.5B263, SUCCESS                            2   EndInstall, CDM_2.5B263, SUCCESS
3   StartPatch, CDM_2.5.0.2B1, OK                             <
4   StartPatch, CDM_2.5.0.3B1, OK                               4   StartPatch, CDM_2.5.0.3B1, OK
5   EndPatch, CDM_2.5.0.3B1, SUCCESS                          | 5   EndPatch, CDM_2.5.0.3B1, SUCCESS

This doesn't output a new file, but you get a vimdiff session that visually compares the two files and highlights the missing lines and characters very intuitively.

$ vimdiff file1.txt file2.txt

  • Hi , this is not solving my problem, the files i posted here are just a small example. What actually i want to is to have a third file in which all the missed and extra enteries can be seen from file1 (file1 is the master file ) – Parul kusmatia Apr 2 '17 at 10:04
  • How does it not solve your problem? Clarify. – projectdp Apr 2 '17 at 10:05
  • The files i pasted here are very small, and clearly the diff is seen, from vim, diff and simple comm. But i need to see what is missed from file2 and in what sequence it is missed , like what shud be above and below it . This thing has to be compared from fike1. I am taking base as file1. – Parul kusmatia Apr 2 '17 at 10:10

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.