Wie bekomme ich diff, um nur hinzugefügte und gelöschte Zeilen anzuzeigen

6933
C. Ross

Wie bekomme ich den Unix-Befehl diff show nur hinzugefügte und gelöschte Zeilen? Wenn diff es nicht kann, welches Werkzeug kann das?

2
Sprechen wir über den Unix-Befehl diff? Johan vor 14 Jahren 1

3 Antworten auf die Frage

6
Craig

Does diff -u0 do what you want?

Der diff auf aix erkennt -u0 (leider) nicht. C. Ross vor 14 Jahren 0
Sie könnten * den GNU-Diff installieren *. dmckee vor 14 Jahren 2
3
Dave Webb

I'm not sure this is possible as it will be hard to differentiate between changed, added and deleted lines.

Consider this file:

start old old old end 

We edit it so it looks like this:

start old old but now new new new end 

If we diff it we get this output:

< old < old --- > old but now new > new > new 

This is straightforward to generate. But if you ask diff to only print added and deleted line I think it becomes a matter of opinion which lines have been added and deleted and which have been changed. For example, did I delete the the last line that said old and replace it with a line that said new or did I edit it?

(+1) Grundsätzlich zustimmen, aber es sollte einen Weg geben, um herauszufinden, welche Unterschiede die hinzugefügten und gelöschten Zeilen meinen. Anders ausgedrückt, zeigen Sie die Ausgabe von `sdiff file1 file2` ohne die Zeilen an, die auf beiden Seiten Einträge haben. Wenn Sie alle geänderten Zeilen als gelöschte und hinzugefügte Zeilen behandeln, erhalten Sie grundsätzlich die Ausgabe von `comm -3 file1 file2` nagul vor 14 Jahren 1
2
Steve

Ich hatte die gleiche Frage. Diese Funktion war meine Lösung, um die maximale Änderungszeilennummer zu erhalten (dh die Änderungen beginnen mit dem Buchstaben '+'). Danach schleife ich die diff-Datei erneut Zeile für Zeile durch und sende sie nicht an den Zeilenprozessor, bis dies die Zeile zur Verarbeitung auslöst:

 #==================================================================== proc_diff_file() # processes a diff file #==================================================================== # Arg_1 = Diff File Name # Arg_2 = New File Name - the one with the new lines { NEW_FILE=$1 A_FILE=$2 if [ -f "$A_FILE" ]; then echo "processing diff $A_FILE" pre_process_diff $A_FILE # Set loop separator to end of line ndx=0 BAKIFS=$IFS IFS=$(echo -en "\n\b") exec 3<&0 exec 0<$A_FILE while read line do ndx=$(expr $ndx + 1) # use $line variable to process line in processLine() function if [ $ndx > $max_ndx ]; then proc_age $line fi done exec 0<&3 # restore $IFS which was used to determine what the field separators are IFS=$BAKIFS  # cleanup $NEW_FILE  echo "processing diff $A_FILE done" fi }  

Hier ist die Funktion:

 #==================================================================== pre_process_diff() # pre-processes a diff file for last changed line # sets a variable named $max_ndx #==================================================================== # Arg_1 = Diff File Name { A_FILE=$1 max_ndx= # collect last line number of diff +  # all lines following are new data `grep -n "^[+]" $A_FILE | gawk '-F:' '{ print $1 }' >tmp` # Set loop separator to end of line # read through to the last line number BAKIFS=$IFS IFS=$(echo -en "\n\b") exec 3<&0 exec 0<tmp while read last_line do max_ndx=$last_line done exec 0<&3 # restore $IFS which was used to determine what the field separators are IFS=$BAKIFS echo "pre-processing diff $A_FILE done max_ndx=$max_ndx" }  

Steve

Installiere GNU diff und verwende das. AIX-Tools sind stark veraltet. vonbrand vor 8 Jahren 0