anol
anol

Reputation: 9115

diff with file names prefixing each line

How can I diff two (or more) files, displaying the file name at the beginning of each line?

That is, instead of this:

--- file1.c
+++ file2.c
@@ -1 +1 @@
-int main() {
+int main(void) {

I would prefer something like this:

file1.c:- int main() {
file2.c:+ int main(void) {

This is not so useful when there are only two files, but extremely handy when using --from-file/--to-file.

Upvotes: 4

Views: 832

Answers (2)

Satya Nemana
Satya Nemana

Reputation: 1

Improved above script by adding more flavors like group-formats and also printed file names as top header, which is the basic need, especially when we run diff on several sub-directories recursively. Also added diff -rwbit to ignore white spaces etc. Please remove this option -wbit if you don't need. Keep -r which is harmless.

I use this quite a lot with git as follows: git difftool -v -y -x mydiff

+ cat mydiff
#!/usr/bin/bash
# the first argument is the original file that others are compared with
orig=$1
len1=${#1}
shift

# we compute the length of the filenames to ensure they are aligned
for arg in "$@"
do
    len2=${#arg}
    maxlen=$((len1 > len2 ? len1 : len2))
    prefix1=$(printf "%-${maxlen}s" "$orig")
    prefix2=$(printf "%-${maxlen}s" "$arg")
    echo -e "\nmydiff $orig $arg =========================\n"
    diff -rwbit \
         --old-line-format="$prefix1:-%L" \
         --new-line-format="$prefix2:+%L" \
     --old-group-format='%df%(f=l?:,%dl)d%dE
%<' \
         --new-group-format='%dea%dF%(F=L?:,%dL)
%>' \
         --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)
%<---
%>' \
         --unchanged-line-format="" $orig $arg
    echo "---" # not necessary, but helps visual separation
done
+ cat -n test1
     1  1st line in test1
     2  2nd line in test1
     3  3rd line in test1
     4  4th line in test1
     5  6th line in test1
+ cat -n test2
     1  1st line in test1
     2  2nd line in test2 changed
     3  3rd line added in test2
     4  4th line in test1
+ mydiff test1 test2

mydiff test1 test2 =========================

2,3c2,3
test1:-2nd line in test1
test1:-3rd line in test1
---
test2:+2nd line in test2 changed
test2:+3rd line added in test2
5d4
test1:-6th line in test1
---

Upvotes: 0

anol
anol

Reputation: 9115

I could not find a more concise solution, so I wrote my own script to do it, using several calls to diff to add a different prefix each time.

#!/bin/bash

# the first argument is the original file that others are compared with
orig=$1
len1=${#1}
shift

# we compute the length of the filenames to ensure they are aligned
for arg in "$@"
do
    len2=${#arg}
    maxlen=$((len1 > len2 ? len1 : len2))
    prefix1=$(printf "%-${maxlen}s" "$orig")
    prefix2=$(printf "%-${maxlen}s" "$arg")
    diff --old-line-format="$prefix1:-%L" \
         --new-line-format="$prefix2:+%L" \
         --unchanged-line-format="" $orig $arg
    echo "---" # not necessary, but helps visual separation
done

Upvotes: 2

Related Questions