0

I have 2 file, a.txt and b.txt and I want to compare them.

a.txt contains:

abc
jkl < jkl
mno > mno
pqr <> pqr

b.txt contains:

abc
jkl < jkl
mno > mno
pqr <> pqrs
stu

I'm using this script:

$ diff a.txt b.txt | grep "> " | cut -c3- > c.txt

Which results in c.txt:

pqr <> pqr
pqr <> pqrs
stu

Why is pqr <> pqr being included in the results? How can I resolve this?

1
  • 3
    grep '^> '. That is, look for > only at the start of the line. What has ssh to do with this? Commented May 28, 2014 at 10:03

3 Answers 3

1

As @StephaneChazelas noted, you forgot to anchor your regular expression, so the "> " will match the > in the line pqr <> pqr that is only in a.txt.

But it looks like you want a filter to only show the lines that are new or changed in b.txt. If you are using GNU diff, it offers you some options to achieve this directly:

$ diff --old-line-format '' --unchanged-line-format '' --new-line-format '%L' a.txt b.txt 
pqr <> pqrs
stu

The --old-line-format '' removes all lines that are not in b.txt, the --unchanged-line-format '' removes all lines common to both files and the --new-line-format '%L' shows just the line contents for all new or changed lines in b.txt. The leading > is suppressed.

1

As @StephaneChazelas stated in the comments, this would appear to have nothing to do with SSH. Running everything locally I get the same results as you, mainly:

$ diff a.txt b.txt | grep "> " | cut -c3-
pqr <> pqr
pqr <> pqrs
stu

The diff command is identifying lines that are different and the line pqr <> pqrs is unique among the 2 files. You can use a side by side diff to see which lines are lining up if it helps.

$ diff -y a.txt b.txt
abc                                                             abc
jkl < jkl                                                       jkl < jkl
mno > mno                                                       mno > mno
pqr <> pqr                                                    | pqr <> pqrs
                                                              > stu

Lines that are similar but not identical are marked with a |. Lines that are unique are marked with a > if only in file #2, or a < if only present in file #1. Identical lines do not show any such marking.

0
0

Maybe you have extra white spaces.

try

diff -w -B file1.txt file2.txt

-w Ignore all white space.

-B Ignore changes whose lines are all blank.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.