Hi,
I am using the below command to get the diff output of two files.
Here in the output file, I wish to add line number in front of each line starting position.
Please suggest the diff options or some command line utilities.
diff -y file1.txt file2.txt --width=300 > diff_f1_f2_300.txt
Thanks in advance.
Manimuthu
RudiC
April 18, 2016, 8:58am
2
Not clear! But, mayhap,
diff -y file1.txt file2.txt --width=300 | nl > diff_f1_f2_300.txt
?
1 Like
hergp
April 18, 2016, 9:27am
3
Or maybe this?
file1:
one
two
three
file2:
one
two
four
and
$ diff --old-line-format="%5dn< %L" --new-line-format="%5dn> %L" --unchanged-line-format="" file1 file2
3< three
3> four
or
$ diff --old-line-format="%5dn< %L" --new-line-format="%5dn> %L" --unchanged-line-format="%5dn= %L" file1 file2
1= one
2= two
3< three
3> four
You can play with the formats to get the results you need. Note: The formatting options are GNU extensions to the diff
command.
3 Likes
Thanks, Here what is the mean for "%5dn< %L"
?
hergp
April 19, 2016, 7:33am
5
%5dn
prints the line number with a standard printf like format of %5d
(numeric, 5 characters wide with leading blanks).
<
or >
are literal strings.
%L
is substituted by the line content.
See also the diff man-page
1 Like
Note: hergp's really good answer will only work with GNU diff - usually what you have with linux.