Development Tip

선행 공백을 무시하면서 2 개의 파일을 어떻게 비교할 수 있습니까?

yourdevel 2020. 12. 12. 12:32
반응형

선행 공백을 무시하면서 2 개의 파일을 어떻게 비교할 수 있습니까?


2 개의 소스 파일이 있는데 동일한 버전이 다릅니다. 그러나 하나는 들여 쓰기를 변경 한 다른 편집기를 통해 이루어 졌으므로 모든 행이 diff에서 다르게 표시됩니다.

출력이 선행 공백 / 탭을 무시한 후 다른 줄만 표시되도록 비교하는 데 사용할 수있는 diff 명령 또는 필터가 있습니까?


diff 유용 할 수있는 몇 가지 옵션이 있습니다.

   -E, --ignore-tab-expansion
          ignore changes due to tab expansion

   -Z, --ignore-trailing-space
          ignore white space at line end

   -b, --ignore-space-change
          ignore changes in the amount of white space

   -w, --ignore-all-space
          ignore all white space

   -B, --ignore-blank-lines
          ignore changes whose lines are all blank

따라서 diff -w old new모든 공백을 무시하고 실질적으로 다른 행만보고해야합니다.


탭을 잘못 사용하는 경우 수정할 수 있습니다.

expand bad_file

diff -bB file[12]
-b, --ignore-space-change
      ignore changes in the amount of white space
-B, --ignore-blank-lines
      ignore changes whose lines are all blank

그주의 -w선 같은 옵션이 diffing의 전에 모든 공백을 무시하는 것 this i s a linethis is a line같이 비교 각 파일 thisisaline과 차이점을보고하지 않습니다.

-w옵션 문제 외에도 옵션에도 -b사소한 문제가 있으며 줄을 구걸 할 때 공백을 무시하지 않습니다.

따라서 sed처음에 발생한 공백을 제거 하기 위해 를 사용해야 하고`diff -bB를 수행해야합니다.

diff -bB <(sed 's/^[ \t]*//' file1) <(sed 's/^[ \t]*//' file2)

참고 URL : https://stackoverflow.com/questions/16423024/how-can-i-diff-2-files-while-ignoring-leading-white-space

반응형