Question:
How do you extract matches between two text files?
secretscp
2010-02-12 10:31:27 UTC
I have two separate lists of items in two different text files. I want to look in text file A for any items that also exist in text file B and then remove them from file A. I thought it would be easier to do but am stuck. I'm running a Mac so would prefer a Unix or Mac solution to the problem. Any ideas?
Three answers:
24 Char Left
2010-02-12 11:13:14 UTC
I don't have much idea about Mac. There is Lazarus for Mac. It is like Delphi for windows. You can start a new project. Place a button on the form, double click on the button and try the code bellow:

//Start of code

procedure TForm1.Button1Click(Sender: TObject);

var

AFile, BFile, CFile: TStringList;

i: integer;

Begin

AFile:= TStringList.Create;

BFile:= TStringList.Create;

CFile:= TStringList.Create;



AFile.LoadFromFile('afile.txt'); // Path to File: A

BFile.LoadFromFile('bfile.txt'); // Path to File: B





//Take the larger file here. Assume AFile is bigger than BFile.

For i := 0 to (AFile.Count - 1) Do

//To do opposite "if BFile.IndexOf(AFile[i]) > -1 then"

if BFile.IndexOf(AFile[i]) = -1 then

CFile.Add(AFile[i]);





CFile.SaveToFile('cfile.txt'); // Save the result File as: C



AFile.Free;

BFile.Free;

CFile.Free;



ShowMessage('OK');

End;



//End of code



After entering the code click on Run -> Run, or press F9

Best of luck :)
jimbot
2010-02-12 10:44:44 UTC
Don't know what language you're using so this is in English:



Load the two lists into two arrays

Sort the arrays (alphabetically or whatever)

Assign a variable for the current index of each array (i is for array 1, j is for array 2) - set each to 1 (or 0 depending on your language)

loop through list 1 (while i < length of list 1):

Compare item i of list 1 to item j of list 2.

if item i = item j

-delete item i of list 1 ('i' will now reference the next element of list 1)

else if item i < item j

-add 1 to j

else

-add 1 to i

end loop
california gardener
2010-02-12 10:59:48 UTC
I think sed, awk and grep would help via the terminal on the mac.



I couldn't give you the exact command line for either but there are great cheat sheets out there for all of them



http://www.catonmat.net/blog/sed-stream-editor-cheat-sheet/


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...