Diff option to get line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Diff option to get line number
# 1  
Old 04-18-2016
Diff option to get line number

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.

Code:
diff -y file1.txt file2.txt --width=300 > diff_f1_f2_300.txt

Thanks in advance.
Manimuthu
# 2  
Old 04-18-2016
Not clear! But, mayhap,
Code:
diff -y file1.txt file2.txt --width=300 | nl > diff_f1_f2_300.txt

?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-18-2016
Or maybe this?

file1:
Code:
one
two
three

file2:
Code:
one
two
four

and
Code:
$ diff --old-line-format="%5dn< %L" --new-line-format="%5dn> %L" --unchanged-line-format="" file1 file2
    3< three
    3> four

or
Code:
$ 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.
These 3 Users Gave Thanks to hergp For This Post:
# 4  
Old 04-19-2016
Thanks, Here what is the mean for "%5dn< %L"?

Last edited by RudiC; 04-19-2016 at 07:35 AM.. Reason: added icode tags
# 5  
Old 04-19-2016
%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
This User Gave Thanks to hergp For This Post:
# 6  
Old 04-19-2016
Note: hergp's really good answer will only work with GNU diff - usually what you have with linux.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. Shell Programming and Scripting

Cut from specific line number to a line number

Hi All, I've a file like this.. Sheet1 a,1 a,2 a,3 a,4 a,5 Sheet2 a,6 a,7 a,8 a,9 a,10 Sheet3 a,11 a,12 a,13 (7 Replies)
Discussion started by: manab86
7 Replies

3. Shell Programming and Scripting

Write $line number into textfile and read from line number

Hello everyone, I don't really know anything about scripting, but I have to manage to make this script, out of necessity. #!/bin/bash while read -r line; do #I'm reading from a big wordlist instructions using $line done Is there a way to automatically write the $line number the script... (4 Replies)
Discussion started by: bobylapointe
4 Replies

4. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

5. Shell Programming and Scripting

Awk - Count instances of a number in col1 and put results in a col2 (new) of diff file

I have 2 files as follows: filename1: : 6742 /welcome/mundial98_ahf1_404.htm 1020 6743 /welcome/mundial98_ahf1_404.htm 2224 6744 /welcome/mundial_ef1_404.htm 21678 6745 /welcome/mundial_if_404.htm 4236 6746 /welcome/mundial_lf1_404.htm 21678 filename2: 6746 894694763 1... (2 Replies)
Discussion started by: jontjioe
2 Replies

6. Shell Programming and Scripting

Diff Command to return the number of lines inserted,deleted and changed.

Hello, I have to compare two files file1 and file2, retrieve the number of lines modified (added, deleted or modified) in file2. Output must be like: File2: Added Deleted Changed Total ------ ------- -------- ----- 2 1 1 4 Somebody... (2 Replies)
Discussion started by: nmattam
2 Replies

7. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

8. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

9. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies

10. Shell Programming and Scripting

how to diff two files line by line

Hi, Is there any way to achieve diff of two files line by line. for eg, a.txt a b c d e b.txt f g h i j (1 Reply)
Discussion started by: Omkumar
1 Replies
Login or Register to Ask a Question