Way of getting common lines between two files

456 views
Skip to first unread message

memco44

unread,
May 1, 2015, 1:17:38 PM5/1/15
to textwr...@googlegroups.com
I know about diffing two files. I'm looking for a way of only seeing the common lines between two files. Is this possible with TextWrangler? BBEdit? Other tools?


Steve

unread,
May 1, 2015, 1:28:50 PM5/1/15
to textwr...@googlegroups.com
No, there is no "show common lines" feature, although I need this feature on occasion and use workarounds to accomplish it.

From the command line, 'fgrep':
    fgrep -xf file1 file2

If you want to do something similar from TextWrangler, I usually just sort the two files individually (removing duplicates), then copy the contents of the first file into the second file at the end.

Sort these results, but this time, DON'T remove duplicates.  You can see which lines were common to both files by searching for:
    ^(.*\n)\1

That is, starting at the beginning of one line, match to the end of the line (including the newline at the end), and then look for the exact same text on the next line. You may need to replace '\n' with '\r' depending on your "line endings" preference.

Neither method is really optimal, but it's usually what I do when I need to find similar lines in different files.

-Steve

Christopher Stone

unread,
May 2, 2015, 12:51:52 PM5/2/15
to TextWrangler-Talk
On May 01, 2015, at 10:34, memco44 <mmerk...@gmail.com> wrote:
I know about diffing two files. I'm looking for a way of only seeing the common lines between two files.
______________________________________________________________________

Hey There,

That can be done easily enough with `diff` or `comm`.

diff --changed-group-format='' --unchanged-group-format='%=' <(sort ~/"Downloads/testDiff 01.txt") <(sort ~/"Downloads/testDiff 02.txt")

comm -12 <(sort ~/"Downloads/testDiff 01.txt") <(sort ~/"Downloads/testDiff 02.txt")

Perl works too, although one-liner might fail if there's not a trailing linefeed in each file.

perl -ne 'print if ($seen{$_} .= @ARGV) =~ /10$/' ~/"Downloads/testDiff 01.txt" ~/"Downloads/testDiff 02.txt"

These work with files on-disk, but it'd be easy enough to make them function on open BBEdit documents (saved or unsaved).

Here's an AppleScript (operating on window 1 and window 2):

-------------------------------------------------------------------------------------------
set commonLinesList to {}

tell application "TextWrangler"
  set doc1Lines to contents of lines of text document 1
  set doc2Lines to contents of lines of text document 2
end tell

repeat with i in doc1Lines
  if i is in doc2Lines then
    set end of commonLinesList to contents of i
  end if
end repeat

set AppleScript's text item delimiters to return
tell application "TextWrangler"
  make new document with properties {name:"Common-Lines-Unsaved", text:commonLinesList as text}
end tell
-------------------------------------------------------------------------------------------

NOTE: I would not use this script with really massive files.

As usual TMTOWTDI.  :)

--
Best Regards,
Chris

Reply all
Reply to author
Forward
0 new messages