Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is it possible to return the line available in FILE1 but not FILE2 in diff?

26 views
Skip to first unread message

Peng Yu

unread,
Nov 12, 2012, 6:19:35 PM11/12/12
to bug-gn...@gnu.org
Hi,

I just want to return the line available in FILE1 but not FILE2 in
diff. After I read the document, it seems that this is not possible.
Could anybody confirm whether this is true? If so, is there an
alternative tool that does this? Thanks!

--
Regards,
Peng

Chris F.A. Johnson

unread,
Nov 12, 2012, 7:47:48 PM11/12/12
to
Perhaps you want comm instead of diff:

comm -23 FILE


--
Chris F.A. Johnson <http://cfajohnson.com>
Author: =======================
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)

Chris F.A. Johnson

unread,
Nov 12, 2012, 8:14:38 PM11/12/12
to
On 2012-11-13, Chris F.A. Johnson wrote:
> On 2012-11-12, Peng Yu wrote:
>> Hi,
>>
>> I just want to return the line available in FILE1 but not FILE2 in
>> diff. After I read the document, it seems that this is not possible.
>> Could anybody confirm whether this is true? If so, is there an
>> alternative tool that does this? Thanks!
>
> Perhaps you want comm instead of diff:
>
> comm -23 FILE

I meant:

comm -23 FILE1 FILE2

Charles Swiger

unread,
Nov 12, 2012, 7:40:01 PM11/12/12
to Peng Yu, bug-gn...@gnu.org
Hi--

On Nov 12, 2012, at 3:19 PM, Peng Yu wrote:
> I just want to return the line available in FILE1 but not FILE2 in
> diff. After I read the document, it seems that this is not possible.
> Could anybody confirm whether this is true? If so, is there an
> alternative tool that does this? Thanks!

Using diff will show removed lines with a "-"; otherwise, consider
using the comm utility since that might be closer to what you want...

% cat > FILE1
This
is
a
test
file.
% tail -4 FILE1 >! FILE2 % diff -bdu FILE1 FILE2
--- FILE1 2012-11-12 16:37:45.000000000 -0800
+++ FILE2 2012-11-12 16:38:11.000000000 -0800
@@ -1,4 +1,3 @@
-This
is
a
test

% comm FILE1 FILE2
This
is
a
test
file.

Regards,
--
-Chuck


John Cowan

unread,
Nov 12, 2012, 8:53:01 PM11/12/12
to Charles Swiger, bug-gn...@gnu.org, Peng Yu
Charles Swiger scripsit:

> Using diff will show removed lines with a "-";

That's diff -u format.

> using the comm utility since that might be closer to what you want...

That only works if the files are both sorted.

--
Using RELAX NG compact syntax to John Cowan <co...@ccil.org>
develop schemas is one of the simple http://www.ccil.org/~cowan
pleasures in life....
--Jeni Tennison <co...@ccil.org>

Bob Proulx

unread,
Nov 13, 2012, 1:49:34 PM11/13/12
to Peng Yu, bug-gn...@gnu.org
Charles Swiger wrote:
> Peng Yu wrote:
> > I just want to return the line available in FILE1 but not FILE2 in
> > diff. After I read the document, it seems that this is not possible.
> > Could anybody confirm whether this is true? If so, is there an
> > alternative tool that does this? Thanks!
>
> Using diff will show removed lines with a "-"; otherwise, consider
> using the comm utility since that might be closer to what you want...

You might consider that -u may take an optional number of context
lines to display.

`-U LINES'
`--unified[=LINES]'
Use the unified output format, showing LINES (an integer) lines of
context, or three if LINES is not given. *Note Unified Format::.
For proper operation, `patch' typically needs at least two lines of
context.

Allowing use like this:

$ printf "AA\nBB\nCC\n" > 1
$ printf "AA\nCC\n" > 2
$ diff -u 1 2
--- 1 2012-11-13 11:37:56.000000000 -0700
+++ 2 2012-11-13 11:38:03.000000000 -0700
@@ -1,3 +1,2 @@
AA
-BB
CC
$ diff -u0 1 2 | sed "1,2d;/^@@/d"
-BB
$ diff -u0 1 2 | sed "1,2d;/^@@/d;s/^-//"
BB

Bob

0 new messages