diff - 라인 번호 출력

diff - 라인 번호 출력

파일 비교를 위해 cli 도구를 사용하고 싶고 줄 차이로 이동할 수 있는 출력 줄 앞에 줄 번호가 필요합니다. 줄이 이렇게 시작하는 경우 점프할 위치를 이해하는 도구를 사용하기 때문입니다.:line-number: regular line contents

그래서 시도했는데 diff문서를 읽는 것이 가능할 것 같습니다.

  -D, --ifdef=NAME                output merged file with `#ifdef NAME' diffs
      --GTYPE-group-format=GFMT   format GTYPE input groups with GFMT
      --line-format=LFMT          format all input lines with LFMT
      --LTYPE-line-format=LFMT    format LTYPE input lines with LFMT
    These format options provide fine-grained control over the output
      of diff, generalizing -D/--ifdef.
    LTYPE is `old', `new', or `unchanged'.  GTYPE is LTYPE or `changed'.
    GFMT (only) may contain:
      %<  lines from FILE1
      %>  lines from FILE2
      %=  lines common to FILE1 and FILE2
      %[-][WIDTH][.[PREC]]{doxX}LETTER  printf-style spec for LETTER
        LETTERs are as follows for new group, lower case for old group:
          F  first line number
          L  last line number
          N  number of lines = L-F+1
          E  F-1
          M  L+1
      %(A=B?T:E)  if A equals B then T else E
    LFMT (only) may contain:
      %L  contents of line
      %l  contents of line, excluding any trailing newline
      %[-][WIDTH][.[PREC]]{doxX}n  printf-style spec for input line number
    Both GFMT and LFMT may contain:
      %%  %
      %c'C'  the single character C
      %c'\OOO'  the character with octal code OOO
      C    the character C (other characters represent themselves)

하지만 이 복잡한 스위치에 대한 예나 설명은 없습니다.

에서 그러한 출력을 얻을 수 있습니까 diff? 그렇다면 어떻게?

답변1

예, 가능합니다. 이러한 옵션을 사용할 때 기본값은 모든 줄을 인쇄하는 것입니다. 이는 매우 장황하며 원하는 내용이 아닙니다.

diff --unchanged-line-format=""

변경되지 않은 라인을 제거하므로 이제 기존 라인과 새 라인만 생성됩니다.

diff --unchanged-line-format="" --new-line-format=":%dn: %L"

이제 접두사와 공백이 붙은 새 줄을 표시 :<linenumber>:하지만 여전히 이전 줄을 인쇄합니다. 당신이 그들을 제거하고 싶다고 가정하면,

diff --unchanged-line-format="" --old-line-format="" --new-line-format=":%dn: %L"

새 줄이 아닌 기존 줄을 인쇄하려면 서로 바꾸십시오.

답변2

때로는 사진이나 예가 1,000 단어의 가치가 있습니다. wnoise위의 답변을 기반으로 두 개의 MySQL(구조) 덤프를 "비교"하기 위해 다음 파이프라인을 구성했습니다 wnoise.

나란히 있는 줄 번호의 예:

 diff   --unchanged-line-format="" --old-line-format="%dn: %L  " --new-line-format="| %dn: %L"  \
         ./20220202-msqldump.sql  ./20221130-msqldump.sql       |
     awk -e' /^[[:digit:]]+: )/{ previous = $0; next; } { print previous $0 }' |
     grep -v -e"ENGINE=InnoDB AUTO_INCREMENT="

diff안타깝게도 do: 옵션이 --side-by-sideline-format 옵션에서 지원되지 않는다는 것을 발견했습니다 .

AUTO_INCREMENT 행을 제거하면 날짜 등 두 가지 차이점만 남았습니다.

필터 가 없으면 grep출력은 다음과 같습니다.

5127: ) 엔진=InnoDB AUTO_INCREMENT=340 기본 문자 집합=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; | 5105: ) 엔진=InnoDB AUTO_INCREMENT=271 기본 문자셋=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 5150: ) 엔진=InnoDB AUTO_INCREMENT=895 기본 문자 집합=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; | 5128: ) 엔진=InnoDB AUTO_INCREMENT=763 기본 문자 집합=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 5170: ) 엔진=InnoDB AUTO_INCREMENT=1371 기본 문자셋=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; | 5148: ) 엔진=InnoDB AUTO_INCREMENT=1173 기본 문자셋=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

줄 번호가 일치하지 않습니다. 나는 meld일이 잘 되고 있는지 먼저 확인하곤 했다.

관련 정보