Linux Diff Get Only Line Number in the Output

diff command to get number of different lines only

Yes you can, and in true Linux fashion you can use a number of commands piped together to perform the task.

First you need to use the diff command, to get the differences in the files.

diff file1 file2

This will give you an output of a list of changes. The ones your interested in are the lines prefixed with a '>' symbol

You use the grep tool to filter these out as follows

diff file1 file2 | grep "^>"

finally, once you have a list of the changes your interested in, you simply use the wc command in line mode to count the number of changes.

diff file1 file2 | grep "^>" | wc -l

and you have a perfect example of the philosophy that Linux is all about.

How to display line numbers in side by side diff in unix?

Below code can be used to display the uncommon fields in two files, side by side.

sdiff -l file1 file2 | cat -n | grep -v -e '($'  

Below code will display common fields along with line numbers in the output.

diff -y file1 file2 | cat -n | grep -v -e '($'  

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 "^\+"

Get lines that start with number

Could you please try following. Written based on description of OP.

your_command | awk '/^[1-9]/'

You could also use grep too like:

your_commamd | grep "^[1-9]"

Explanation:

In awk in case we have to use a regex to match in current line we use that regex inside / that's why giving ^[1-9] means look if line starts from anything between 1 to 9 and if regex matches for current line as it's by default action printing of current line happens.

Git diff with line numbers (Git log with line numbers)

You can't get human-readable line numbers with git diff

There aren't currently any options to get line-numbers displayed vertically on the side with git diff.

Unified-diff format

That information is available in the (c)hunk headers for each change in the diff though, it's just in unified-diff format:

@@ -start,count +start,count @@

The original state of the file is represented with -, and the new state is represented with + (they don't mean additions and deletions in the hunk header. start represents the starting line number of each version of the file, and count represents how many lines are included, starting from the start point.

Example

diff --git a/osx/.gitconfig b/osx/.gitconfig
index 4fd8f04..fcd220c 100644
--- a/osx/.gitconfig
+++ b/osx/.gitconfig
@@ -11,7 +11,7 @@ <== HERE!
[color "branch"]
upstream = cyan
[color "diff"]
- meta = yellow
+ meta = cyan
plain = white dim
old = red bold
new = green bold

The hunk header

@@ -11,7 +11,7 @@

says that the previous version of the file starts at line 11, and includes 7 lines:

11  [color "branch"]
12 upstream = cyan
13 [color "diff"]
14 - meta = yellow
14 + meta = cyan
15 plain = white dim
16 old = red bold
17 new = green bold

while the next version of the file also starts at line 11, and also includes 7 lines.

Unified-diff format isn't really for human consumption

As you can probably tell, unified-diff format doesn't make it easy to figure out line numbers (at least if you're not a machine). If you really want line numbers that you can read, you'll need to use a diffing tool that will display them for you.

Additional Reading

  • Official git-diff(1) Manual Page

How to display only different rows using diff (bash)

a.txt:

1;john;125;3
1;tom;56;2
2;jack;10;5

b.txt:

1;john;125;3
1;tom;58;2
2;jack;10;5

Use comm:

comm -13 a.txt b.txt 
1;tom;58;2

The command line options to comm are pretty straight-forward:

-1 suppress column 1 (lines unique to FILE1)

-2 suppress column 2 (lines unique to FILE2)

-3 suppress column 3 (lines that appear in both files)



Related Topics



Leave a reply



Submit