How to Understand Diff -U in Linux

Understand Output of diff -u

Yes, the diff -u command you used means "unified diff", so the -392,7 refers to the first file (the - aka file_x) starting from line 392 where 7 lines are shown and similarly +392,7 refers to the second file (the + aka file_y) starting from line 392 where 7 lines are shown.

unix diff side-to-side results?

From man diff, you can use -y to do side-by-side.

-y, --side-by-side
output in two columns

Hence, say:

diff -y /tmp/test1  /tmp/test2

Test

$ cat a                $ cat b
hello hello
my name my name
is me is you

Let's compare them:

$ diff -y a b
hello hello
my name my name
is me | is you

What does @@ -1 +1 @@ mean in Git's diff output?

It's a unified diff hunk identifier. This is documented by GNU Diffutils.

The unified output format starts with a two-line header, which looks like this:


--- from-file from-file-modification-time
+++ to-file to-file-modification-time

The time stamp looks like 2002-02-21 23:30:39.942229878 -0800 to indicate the date, time with fractional seconds, and time zone. The fractional seconds are omitted on hosts that do not support fractional time stamps.

You can change the header's content with the --label=label option; see See Alternate Names.

Next come one or more hunks of differences; each hunk shows one area where the files differ. Unified format hunks look like this:


@@ from-file-line-numbers to-file-line-numbers @@
line-from-either-file
line-from-either-file...

If a hunk contains just one line, only its start line number appears. Otherwise its line numbers look like start,count. An empty hunk is considered to start at the line that follows the hunk.

If a hunk and its context contain two or more lines, its line numbers look like start,count. Otherwise only its end line number appears. An empty hunk is considered to end at the line that precedes the hunk.

The lines common to both files begin with a space character. The lines that actually differ between the two files have one of the following indicator characters in the left print column:

  • +
    A line was added here to the first file.
  • -
    A line was removed here from the first file.

How to get the difference (only additions) between two files in linux

diff and then grep for the edit type you want.

diff -u A1 A2 | grep -E "^\+"

How to check if a diff command is successful in Unix shell script

There are many ways to fix your script, but this is probably the most minimal:

a=$(cat a)
if [ "$a" = "" ]; then
  1. Variables are different from files, so you have to read the output of the file you created.
  2. Variables that potentially contain spaces must be quoted when used as arguments, and strings must be compared against empty rather than 0.

(As suggested in the comment, if you don't need the diff output, you could just use the exit code.)

Unix diff side-by-side with context

It seems that diff(1) doesn't allow it, but vimdiff seems to work:

vimdiff -c 'set diffopt=context:3' file1 file2

Downside is that it's interactive-only, you can't dump the diff to a file, but then again, side-by-side diffs are not very useful in files. However, if you do want to save it to a file, this awesome answer will have you do:

vimdiff -c 'set diffopt=context:3' -c TOhtml -c 'w! output.html' -c 'qa!' file1 file2

Not ideal, but it's something.

unix diff to file

First, you probably need to identify the encoding of the Localizable.strings files. This might be done in a manner described by How to find encoding of a file in Unix via script(s), for example.

Then probably you need to convert the Localizable.strings file to UTF-8 with a tool like iconv using commands something like:


iconv -f x -t UTF-8 $PROJECT_DIR/new/Localizable.strings >Localizable.strings.new.utf8
iconv -f x -t UTF-8 $PROJECT_DIR/old/Localizable.strings >Localizable.strings.old.utf8

Where x is the actual encoding in a form recognized by iconv. You can use iconv --list to show all the encodings it knows about.

Then, you probably need to diff without having to use -a.


diff -u -B Localizable.strings.old.utf8 Localizable.strings.new.utf8 >Localizable.strings.diff.utf8


Related Topics



Leave a reply



Submit