Remove lines from one file that exist in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove lines from one file that exist in another file
# 1  
Old 09-14-2012
Remove lines from one file that exist in another file

Hello Everyone,

I'm currently have a requirement where I've generated a list of files with specific attributes and I need to know what lines are similar between the two files. For example:

-File 1-
line1
line2
line3

-File 2-
line1
line2
line4
line5

-Desires Output-
line1
line2

Is there a simple command that will let be view the similarities between files? Basically something the opposite of diff.

Below is a copy of the bash script I've been working on so far. The problem with the script so far is that it doesn't work when there "grep -v" excludes the first line from output when scanning the target file the first time, but the same line would be included again when grep does the same for the second line. So in the end, after sort and uniq, the output is basically identical to the original target file.

I'm eager to know what you guys think!

Code:
#Obtain diff between two files
diff $1 $2 | grep ">" | awk '{ print $2 }' >> exclusions;
diff $1 $2 | grep ">" | awk '{ print $2 }' > ./Records/Exclusions_-_"$1"_-_"$2"

#Read exclusions and inverse grep against target file
while IFS= read line; do grep -v "$line" $2; done < exclusions >> out

#clean up output
cat out | sort | uniq > ./Output/Output_-_"$1"_-_"$2"

#clean up files
cp exclusions exclusions.tmp; cat exclusions | sort | uniq > exclusions; rm -f exclusions.tmp out

# 2  
Old 09-14-2012
Code:
awk 'FNR==NR{a[$0]=1;next}$0 in a' file1 file2

# 3  
Old 09-14-2012
Quote:
Originally Posted by omnivir
Is there a simple command that will let be view the similarities between files?
If the files are sorted:
Code:
comm -12 file1 file2

Regards,
Alister
# 4  
Old 09-14-2012
Try...
Code:
grep -f file1 file2

# 5  
Old 09-14-2012
Quote:
Originally Posted by Ygor
Code:
grep -f file1 file2

If the goal is to match identical lines, I would recommend:
Code:
grep -xFf file1 file2

Using fixed string, whole-line matching will prevent false positives (fixed substrings or regexp wildcard matches), false negatives (some regexps don't match themselves, e.g. [a]), and syntax errors from lines that aren't valid regular expressions.

Regards,
Alister

Last edited by alister; 09-14-2012 at 10:47 AM..
# 6  
Old 09-14-2012
Awesome! I got it working! Smilie Thanks a lot guys. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove lines from File.A based on criteria in File.B

Hello, I have two files of the following form. I would like to remove from File.A where the first three colum matches values in File.B to give the output in File.C File.A 121 54321 PQR CAT 122 765431 ABC DOG 124 98765 ZXY TIGER 125 86432 GEF LION File.B 122 765431 ABC 125 86432 GEF... (4 Replies)
Discussion started by: Gussifinknottle
4 Replies

2. Shell Programming and Scripting

awk remove/grab lines from file with pattern from other file

Sorry for the weird title but i have the following problem. We have several files which have between 10000 and about 500000 lines in them. From these files we want to remove lines which contain a pattern which is located in another file (around 20000 lines, all EAN codes). We also want to get... (28 Replies)
Discussion started by: SDohmen
28 Replies

3. Shell Programming and Scripting

ksh Loop through file one, remove lines from file two

Good Afternoon, I start with a file named biglist.txt. I have another file smallerlist. txt I want to remove the lines from smallerlist.txt from biglist.txt and leave those lines that do not reside in smallerlist.txt. Thanks !! (2 Replies)
Discussion started by: popeye
2 Replies

4. UNIX for Dummies Questions & Answers

Want to remove all lines but not latest 50 lines from a file

Hi, I have a huge file which has Lacs of lines. File system got full. I want your guys help to suggest me a solution so that I can remove all lines from that file but not last 50,000 lines. I want solution which can remove lines from existing file so that I can have some space left with. (28 Replies)
Discussion started by: prashant2507198
28 Replies

5. Shell Programming and Scripting

Code to remove files when corresponding file doesnt exist isnt working.

I am trying to add some code to the begging of a script so that it will remove all the .transcript files, when their is no coressponding .wav file. But it doesnt work. This is the code I have added: for transcriptfile in `$voicemaildir/*.transcript`; do wavfile=`echo $transcriptfile | cut -d'.'... (2 Replies)
Discussion started by: ghurty
2 Replies

6. Shell Programming and Scripting

How to remove blank lines in a file and save the file with same name?

I have a text file which has blank lines. I want them to be removed before upload it to DB using SQL *Loader. Below is the command line, i use to remove blank lines. sed '/^ *$/d' /loc/test.txt If i use the below command to replace the file after removing the blank lines, it replace the... (6 Replies)
Discussion started by: vel4ever
6 Replies

7. Shell Programming and Scripting

Remove duplicate lines from first file comparing second file

Hi, I have two files with below data:: file1:- 123|aaa|ppp 445|fff|yyy 999|ttt|jjj 555|hhh|hhh file2:- 445|fff|yyy 555|hhh|hhh The records present in file1, not present in file 2 should be writtent to the out put file. output:- 123|aaa|ppp 999|ttt|jjj Is there any one line... (3 Replies)
Discussion started by: gani_85
3 Replies

8. Shell Programming and Scripting

Deleting lines of a file if they exist in another file

I have a reference file that needs to remain static and another file that may or may not have duplicate rows that match the reference file. I need help with a command that will delete any duplicate rows from the second file while leaving reference file intact For example reference file would... (4 Replies)
Discussion started by: bjdamon
4 Replies

9. Shell Programming and Scripting

Removing Lines if value exist in first file

I tried a few ways to resolve this using a bash script w/ a loop, no luck. File1: roughly 6,000 account numbers such as: 1111 1512 1113 123 I also have a dozen or so csv files, w/ the account number in the 4th field. What I would like to do is remove all lines if the... (19 Replies)
Discussion started by: svn
19 Replies

10. Shell Programming and Scripting

If doc file exist remove

I need help running a script. I have the script looking into a folder and converting .doc files to .odt. The script works fine except that I want it to only run when .doc files are present. If I can do this then I can put .xls files and .ppt files in the folder and convert them when they are... (2 Replies)
Discussion started by: handband2
2 Replies
Login or Register to Ask a Question