[Techtalk] finding added lines in a file

Miriam English mim at miriam-english.org
Fri Aug 16 08:31:11 UTC 2013


Thanks Bonnie. That's very cool. I wouldn't have thought of using grep 
like that.

One problem I've noticed with my solution and yours is that if a new 
line is added to B that happens to be the same as an existing line 
elsewhere in A it is lost. For example, say A and B are revisions of a 
story and both versions have a line '"Yes," she said.' early in both 
files, if there is another new line added to B later with the same 
wording, but unrelated to the original, so it is truly a new line, it is 
nevertheless lost. It is, of course, because of the sort, which removes 
context, which is why I've been trying to fathom the manuals of cmp, 
diff, and comp. I normally like reading manuals...

Cheers,

	- Miriam


Bonnie King wrote:
> grep can take a list of patterns separated by newlines.
>
> grep -F "$(cat A.txt B.txt |sort |uniq -u)" B.txt
>
> You'll get the added lines back in the order they appear in B.txt.
>
>
>
>
> On Thu, Aug 15, 2013 at 6:59 PM, Miriam English <mim at miriam-english.org
> <mailto:mim at miriam-english.org>> wrote:
>
>     Hi folks,
>
>     Forgive my thickness. I've read (or attempted to read) the manual
>     pages for diff, cmp, and comp and still can't work out how to list
>     just the lines added to file B when comparing two files, A and B.
>     Perhaps I can blame having just getting out of hospital after a bout
>     with a mystery high fever, but I do remember having difficulty
>     trying to work this out before too.
>
>     Anybody know the secret?
>
>       - compare two files, A and B
>       - just print the additional lines of file B
>
>     It should be easy, right? I'm completely stumped.
>
>     I have used the two-step
>       cat A.txt B.txt | sort | uniq -u >C.txt
>       cat C.txt B.txt | sort | uniq -d
>     but this uses a temporary file (C.txt) and upsets the order of the
>     lines in the original (B.txt) when it sorts them.
>
>     Cheers,
>
>              - Miriam
>     --
>     If you don't have any failures then you're not trying hard enough.
>       - Dr. Charles Elachi, director of NASA's Jet Propulsion Laboratory
>     -----
>     Website: http://miriam-english.org
>     Blogs: http://miriam-e.dreamwidth.org
>     http://miriam-e.livejournal.__com <http://miriam-e.livejournal.com>
>
>     _________________________________________________
>     Techtalk mailing list
>     Techtalk at linuxchix.org <mailto:Techtalk at linuxchix.org>
>     http://mailman.linuxchix.org/__mailman/listinfo/techtalk
>     <http://mailman.linuxchix.org/mailman/listinfo/techtalk>
>
>
>
>
> --
> Bonnie King

-- 
If you don't have any failures then you're not trying hard enough.
  - Dr. Charles Elachi, director of NASA's Jet Propulsion Laboratory
-----
Website: http://miriam-english.org
Blogs:   http://miriam-e.dreamwidth.org
          http://miriam-e.livejournal.com




More information about the Techtalk mailing list