Compare two files using shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two files using shell scripting
# 1  
Old 01-02-2012
Compare two files using shell scripting

Hi,

I need to compare two files using shell scripting.
Say like:

File1
AAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCCCCCCCCCCC
eeeeeeeeeeeeeeeeeeeeeeeee
DDDDDDDDDDDDDDDDDDDDDDDDDDDD

File2
BBBBBBBBBBBBBBBBBBBBB
DDDDDDDDDDDDDDDDDDDDDDDDDDDD
AAAAAAAAAAAAAAAAAAAA

output should be:
CCCCCCCCCCCCCCCCCCCCCCCCC
eeeeeeeeeeeeeeeeeeeeeeeee

output should be all lines unique to file1.

I have tried to do this using sort and comm command and it works.
However the problem is that output comes in a sorted manner.
I need to maintain the order in which they appear in file1.
If it can be done using awk, please let me know.
Its kind of urgent.

Thanks,
Rosh
# 2  
Old 01-02-2012
Based on the input given ..
Code:
$ fgrep -vf file2 file1
CCCCCCCCCCCCCCCCCCCCCCCCC
eeeeeeeeeeeeeeeeeeeeeeeee

# 3  
Old 01-02-2012
Code:
grep -v -f file2 file1

https://www.unix.com/shell-programmin...hird-file.html

--ahamed
# 4  
Old 01-02-2012
Hi,

Thanks a lot. At initial glance, it looks to be working.
Let me test some more scenarios.

Btw, how does this work?

Thanks,
Rosh
# 5  
Old 01-02-2012
man grep

Quote:
-v, --invert-match
Invert the sense of matching, to select non-matching lines.
Quote:
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore
matches nothing.
--ahamed
# 6  
Old 01-02-2012
Hi,

I know we can give multiple arguments to grep which will then search in all the files (assuming there is no -v option)
what will it do in case i give -f option?

Thanks,
Rosh
# 7  
Old 01-02-2012
grep -v -f file2 file1 - the contents of the file file1 will be used for searching the target file file2 and display the result. -v will invert the search results.

HTH
--ahamed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to compare to values returned from sql in shell scripting?

hey i am using this code to connect to sql , store the value in variable and then compare it with another variable after some time by executing the same query but the desired result is not coming #!/bin/bash val=$(sqlplus -s rte/rted2@rel76d2 <<ENDOFSQL set heading off set feedback off... (11 Replies)
Discussion started by: ramsavi
11 Replies

2. Shell Programming and Scripting

Compare files using Unix scripting

I have a file containing the below data obtained after running a diff command > abc 10 < abc 15 > xyz 02 <xyz 05 ..... Does anyone know how i can obtain output like : previous value of abc is 10 and present value is 15 similarly for all the comparisons in the text file (10 Replies)
Discussion started by: amithpatrick1
10 Replies

3. Shell Programming and Scripting

Compare two files using shell script

Hi i want to compare two files and i need the o/p of only difference here the files file1 achilles aedxbepo aedxbwdm01 aedxbwdm02 albedo amarice ambrister anakin anton argon artephius asgard avatar aymara (10 Replies)
Discussion started by: venikathir
10 Replies

4. Shell Programming and Scripting

Shell script to compare two files

I have two files; file A and file B. I need all the entries of file A to be compared with file B line by line. If the entry exists on file B, then save those on file C; if no then save it on file D Note :- all the columns of the lines of file A need to be compared, except the last two columns... (8 Replies)
Discussion started by: ajiwww
8 Replies

5. Shell Programming and Scripting

Shell Scripting: Compare pattern in two files and merge the o/p in one.

one.txt ONS.1287677000.820.log 20Oct2010 ONS.1287677000.123.log 21Oct2010 ONS.1287677000.456.log 22Oct2010 two.txt ONS.1287677000.820.log:V AC CC EN ONS.1287677000.123.log:V AC CC EN ONS.1287677000.820.log:V AC CC EN In file two.txt i have to look for pattern which column one... (17 Replies)
Discussion started by: saluja.deepak
17 Replies

6. Shell Programming and Scripting

Shell scripting for this sequence to compare

I have two input files (given below) and to compare each line of the File1 with each line of File2 starts with '>sample1'. If a match occurs and that matched line in the File2 contains another line or sequence of lines starting with "Chr" they have to be displayed in output file with that sample.... (4 Replies)
Discussion started by: hravisankar
4 Replies

7. Shell Programming and Scripting

Shell Script to Compare Two Files

I have a directory with about 6 files that we receive regularly. these 6 files contain information for 3 different units, 2 for each unit. files related to a specific unit are named similarly with a change in number at the end of the file. the numbers should be sequential. for each grouping of... (3 Replies)
Discussion started by: scriptman237
3 Replies

8. Shell Programming and Scripting

How to compare a command line parameter with -- in shell scripting

Hi, I need to check if a parameter provided at the command line is equal to --.How can i do that ? Please help me. Thanks and Regards, Padmini (4 Replies)
Discussion started by: padmisri
4 Replies

9. Shell Programming and Scripting

Bash scripting to compare N number of files located in two directories

I want to compare "N" (around 2000+) number of huge files located in a directory A against "N" files located in a different directory using Bash scripting. Please help me with any scripts available. Thanks. (2 Replies)
Discussion started by: Sangtha
2 Replies

10. UNIX and Linux Applications

How to compare two files using shell script

hi experts please help me to compare two files which are in different directory file1<file will be master file> (/home/rev/mas.txt} ex x1 x2 file2 <will be in different folder> (/home/rev/per/.....) ex x3 x4 the filesinside per folder i need to compare with master file... (1 Reply)
Discussion started by: revenna
1 Replies
Login or Register to Ask a Question