Comparing text in 2 files and output difference in another file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing text in 2 files and output difference in another file.
# 1  
Old 04-12-2012
Comparing text in 2 files and output difference in another file.

I have 2 files of almost same text apart from 2,3 ending lines. Now I want to get that difference in another file.
e.g file1.txt is

Code:
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_livecd-lv_root
                       18G  2.4G   15G  14% /
tmpfs                 504M  264K  504M   1% /dev/shm
/dev/sda1             485M   32M  429M   7% /boot

and file2.txt is:

Code:
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_livecd-lv_root
                       18G  2.4G   15G  14% /
tmpfs                 504M  264K  504M   1% /dev/shm
/dev/sda1             485M   32M  429M   7% /boot
/dev/sdb1             7.5G  249M  7.3G   4% /media/alpha
/dev/sdc1             3.8G  1.1G  2.8G  28% /media/9C6F-1ECD


Now I want to get ending 2 lines in another file say file3.txt that is basic difference in this file.

Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 04-12-2012 at 05:24 PM.. Reason: modified.
# 2  
Old 04-12-2012
use
Code:
diff file1 file2 > mismatchfiles

# 3  
Old 04-12-2012
getting some extra words

thanks for your reply. it is working but not perfectly. I'm getting some extra words in mismatchfile

Here is output of mismatchfile

Code:
6,7d5
< /dev/sdb1             7.5G  249M  7.3G   4% /media/alpha
< /dev/sdc1             3.8G  1.1G  2.8G  28% /media/9C6F-1ECD

now this 6,7d5, < , words are extra in output file. can you please tell me why I'm getting them. Thanks.
# 4  
Old 04-12-2012
They're information on the differences between the files (line numbers in both files, deleted marker, and what the lines were). Check your system documentation for diff (man diff).
# 5  
Old 04-12-2012
Here is the code:

Code:
$$ diff file1 file2 | grep "^> " | cut -c 3- > mismatchfile

---------- Post updated at 02:38 AM ---------- Previous update was at 02:33 AM ----------

Code:
$$ diff file1 file2 | grep "^> " | cut -c 3- > mismatchfile

Ofcourse , CarloM is right. diff command tells us the differences between two files.So if we remove 6,7d5 or any such information from output the remaining information will be useless.Although the above code completes the asked requirement.

sorry friends...I made a mistake...I didnot paid attention on the line "can you please tell me why I'm getting them" of post #3.That is why I have written above not required code.
Now
Here is an explanation of output of diff command:
Code:
0a1,2
> line1
> line2

means append line1 and line2 after line 0 of first file
Code:
2c4
< linex                     ---> line 2 of first file
--
> liney                     ----> line 4 of second file

means change line 2 of first file with line 4 of 2nd file
and
Code:
4d5
< linez                     ---> line 4 of first file

means delete line 4 of first file

so, here a means append, c means change and d means delete
number before a or d or c refers to line number in file1
number before a or d or c refers to line number in file2

Finally, if you follow the modifications suggested by diff, then you are going to make both queried files identical.

Last edited by Epsilon; 04-12-2012 at 06:47 PM..
# 6  
Old 04-12-2012
as mentioned by @47shailesh, its the description from diff, what you can do is remove those descriptions,
Code:
diff file1 file2 | sed -ne 's/^[><] //p' > mismatchfile

# 7  
Old 04-12-2012
Thank You very much all for spending so much time and giving me correct answer. thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. Shell Programming and Scripting

Comparing two files with numbers and taking difference in third file

Hi All, I have two files in the following format, with numbers being defined under columns(described by a set of headers) and rows(again defined by a set of identifiers) 2013 2013 Make200 Make201 Merc BMW Jpur Del ... (9 Replies)
Discussion started by: dev.devil.1983
9 Replies

3. Shell Programming and Scripting

Comparing 2 text files & downloading a file if the last lines are different

Hello I'm having a little difficulty in writing a shell script for a few simple tasks. First I have two files "file1.txt" and "file2.txt" and I want to read and compare the last line of each file. The files look like this. File1.txt File2.txt After comparing the two lines I would... (2 Replies)
Discussion started by: RustikGaming
2 Replies

4. Shell Programming and Scripting

Comparing 2 CSV files and sending the difference to a new csv file

(say) I have 2 csv files - file1.csv & file2.csv as mentioned below: file1.csv ID,version,cost 1000,1,30 2000,2,40 3000,3,50 4000,4,60 file2.csv ID,version,cost 1000,1,30 2000,2,45 3000,4,55 6000,5,70 ... (1 Reply)
Discussion started by: Naresh101
1 Replies

5. Shell Programming and Scripting

Compare two text files and output difference

Hi experts, I am trying to compare two text files and output the difference to another file. I'm not strictly looking for differences in text but additional text at the end of one file that isn't in another, so basically comparing the file 2 against file 1 and printing any additional text to... (9 Replies)
Discussion started by: martin0852
9 Replies

6. Shell Programming and Scripting

Comparing 2 difference csv files

Hello, I have about 10 csv files which range from csv1 - csv10. Each csv file has same type/set of tabs and we have around 5-6 tabs for each of the csv file which have slightly different content(data). A sample of CSV1 is shown below: Joins: Data related to Joins, it can be any number of... (2 Replies)
Discussion started by: bobby1015
2 Replies

7. Shell Programming and Scripting

Comparing a text file's entries with ps -ef output

Shell : Korn os : AIX This is the ps output looking for a process called pmon. pmon runs with various 'service' names which is appended with an underscore as shown below. For example a pmon process for the service hexjkm will be named ora_pmon_hexjkm $ ps -ef | grep pmon oracle... (1 Reply)
Discussion started by: kraljic
1 Replies

8. Shell Programming and Scripting

comparing in files and output

i have a file a with contents tom lasole jon gille sam and a file b with contents tom|1234|abcf|newyork,ohio,oregon sam|2345|drft|texas,london hyle|4444|befr|wisconsin neyo|2333|tdtt|ohio,jersey i want to compare records in file a to file b such that when the name tom is... (5 Replies)
Discussion started by: dealerso
5 Replies

9. Shell Programming and Scripting

comparing to text files

Hi All, I have two files of the following formats file 1 - this is a big file >AB_1 gi|229194403|ref|ZP_04321208.1| group II intron reverse transcriptase/maturase gdfjafhlkhlnlklaklskckcfhhahgfahajfkkallalfafafa >AB_2 gi|229194404|ref|ZP_04321209.1| gfksjgfkjsfjslfslfslhf >AB_3... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

10. Shell Programming and Scripting

Comparing Columns and printing the difference from a particular file

Gurus, I have one file which is having multiple columns and also this file is not always contain the exact columns; sometimes it contains 5 columns or 12 columns. Now, I need to find the difference from that particular file. Here is the sample file: param1 | 10 | 20 | 30 | param2 | 10 |... (6 Replies)
Discussion started by: buzzusa
6 Replies
Login or Register to Ask a Question