Friday, July 11, 2014

Unix Shell: Compare Files(1)

1. cmp command
o1:
 Hello New York!  

o2:
 Hello New York!  
 Hello Chicago!     

o3:
 Boston is awesome!  
 New York is awesome!  
 Los Angeles is awesome!  

o4:
 Hello Boston!  

terminal:
1. Compare o1 with o2, since o2 is longer than o1 and first line is same as o1. So cmp complains that it reach to EOF on o1 when doing the comparison byte by byte.
Note: In the real life, if o1 and o2 "looks" the same, but o1 or o2 has one or more new line in the end, it will still cause this complain, since although new line is invisible, but it is still counted as one valid byte.
2. Compare o2 with o4, it complains that first difference occurs at first line, byte 7.
3. Compare o4 with o3, since they are totally different, it complains that the first difference occurs at line 1 byte 1
4. Compare o1 with o1, since we are comparing the same file, it output nothing indicating that two files are identical.
5 - 6) If we are only interested in the comparison result, we can use -s option to suppress any output, then use "echo $?" to check the return code. 1 means not identical, 0 means identical. In this case o1 is different from o2.
7 - 8) Similar as above, in this case o1 is identical from o1.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cmp o1 o2  
 cmp: EOF on o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cmp o2 o4  
 o2 o4 differ: byte 7, line 1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cmp o4 o3  
 o4 o3 differ: byte 1, line 1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cmp o1 o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cmp -s o1 o2  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo $?  
 1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cmp -s o1 o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo $?  
 0  

2. diff command
terminal:
1) Output the file content of o1
2) Output the file content of o2, everything is same from o1 but there is no newline at the end of string
3) Output the file content of o3, notice that there is still no newline at the end of last line
4) Output the file content of o4, notice that there is no newline at the end of line
5) Use diff command to check the difference between o1 and o2.
Here is the explanation of this: "1c1", c means change, 1 at the left side means line number of o1, 1 at the right side means line number of o2. "<" means following content is from file o1(file at the left side), ">" means following content is from file o2(file at the right side). That last line indicates that "No newline at end of file"
6) Use diff command to check the difference between o1 and o3.
"1c1,3" means change take place at line 1 of left file(o1), and line 1 to 3 of right file(o3). Then it lists file content of above lines.
7) Use diff command check the difference between o1 and o4.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat o1  
 Hello New York!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat o2  
 Hello New York!aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat o3  
 Boston is awesome!  
 New York is awesome!  
 Los Angeles is awesome!aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat o4  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ diff o1 o2  
 1c1  
 < Hello New York!  
 ---  
 > Hello New York!  
 \ No newline at end of file  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ diff o1 o3  
 1c1,3  
 < Hello New York!  
 ---  
 > Boston is awesome!  
 > New York is awesome!  
 > Los Angeles is awesome!  
 \ No newline at end of file  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ diff o1 o4  
 1c1  
 < Hello New York!  
 ---  
 > Hello Boston!  
 \ No newline at end of file  

3. patch command
terminal:
1) Print out file o1's content
2) Print out file o3's content, note that there is no new line at the end of last line.
3) Use "diff -c o1 o3" command to get the difference report of two files, -c option is used to generate a report which could be used to feed patch command
4) Print out the content of difference report:
"******" represents the content of first file o1
"------" represents the content of second file o3
"*** 1 ****" represents the line number (1) of first file o1
"--- 1,3 ----" represents the line number (1 to 3) of second file o3
5) Use patch command to make o1 a patch which make its content same as o3. We use above difference report, it will make patch command to patch against o1 based on o3. -b option is used to back up the o1 before doing the patch
6) print out o1.orig, which is the backup file. Its content is same as original o1 file
7) print out o1, which is the file getting the patch, Its content is same as o3 now.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat o1  
 Hello New York!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat o3  
 Boston is awesome!  
 New York is awesome!  
 Los Angeles is awesome!aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ diff -c o1 o3 >o13.diff  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat o13.diff  
 *** o1    2014-07-09 21:16:14.382998534 -0400  
 --- o3    2014-07-11 08:08:38.287499257 -0400  
 ***************  
 *** 1 ****  
 ! Hello New York!  
 --- 1,3 ----  
 ! Boston is awesome!  
 ! New York is awesome!  
 ! Los Angeles is awesome!  
 \ No newline at end of file  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ patch -b <o13.diff  
 patching file o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt  
 total 16  
 -rw-rw-r-- 1 aubinxia aubinxia 16 Jul 9 21:16 o1.orig  
 -rw-rw-r-- 1 aubinxia aubinxia 63 Jul 11 08:08 o3  
 -rw-rw-r-- 1 aubinxia aubinxia 242 Jul 12 09:17 o13.diff  
 -rw-rw-r-- 1 aubinxia aubinxia 63 Jul 12 09:17 o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat o1.orig  
 Hello New York!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat o1  
 Boston is awesome!  
 New York is awesome!  
 Los Angeles is awesome!aubinxia@aubinxia-fastdev:~/Desktop/xxdev$   

No comments:

Post a Comment