파일에서 줄을 찾아 읽은 다음 주석 처리된 줄을 확인하세요.

파일에서 줄을 찾아 읽은 다음 주석 처리된 줄을 확인하세요.

텍스트 파일 처리가 막혔는데 이것이 제가 하고 싶은 일입니다.

스크립트를 사용하여 git 패치 파일을 생성했는데 패치 파일이 크고 많은 변경 사항이 주석에만 있었습니다. 를 사용하여 큰 패치 파일을 분할한 my/patch/folder다음 The only one in을 사용하여 각 분할 패치를 확인했습니다 splitdiff. find주석을 확인하기 위해 별도의 쉘 스크립트를 만들었습니다.

comments.sh

#!/bin/bash

file=$1

cat $file | grep ^+ | grep -v ^+++ | tr -d " " | tr -d "\t" | cut -c2-3 | while read -r line ; do
    if [ $line != "//" ] ; then
        exit 0
    fi
done

cat $file | grep ^- | grep -v ^--- | tr -d " " | tr -d "\t" | cut -c2-3 | while read -r line ; do
    if [ $line != "//" ] ; then
        exit 0
    fi
done

exit 1

이 스크립트를 사용하여 변경된 모든 줄이 이와 같은지 확인하여 +// this is comment변경 사항이 주석에만 있음을 알고 싶습니다.

그런 다음 다음 스크립트를 실행합니다.

#!/bin/bash

rm -f small.patch
touch small.patch

find my/patch/folder -type f
-print0 | xargs -0 sh -c '
for i
do
  ./comments.sh "$i"
  [ $? -eq 0 ] && cat "$i" >> small.patch
done
' _

하지만 종료 코드는 항상 ./comments.sh "$i"1인 것 같습니다. 이유는 모르겠지만 결국 동일한 큰 패치 파일을 생성하게 됩니다.

도와주세요, 감사합니다!

답변1

몇 가지 예비적인 생각.

while read루프를 사용하는 것은 say를 사용하는 것보다 느립니다.grep -qv '//'

cat | grep | grep | TR | cut |grep그냥 사용하는 것에 비해 속도가 느려집니다 sed.

실제 데이터가 없으면 문제가 무엇인지 말하기가 어렵습니다. 이제 문제를 변경하고 쉽게 테스트할 수 있는 대안을 찾아보겠습니다.

분명히 나는 ​​귀하의 데이터가 없기 때문에 이것을 테스트하지 않았습니다.

이제 새로운 noncomments.sh 스크립트를 만들어 보겠습니다. 주석이 아닌 줄이 반환됩니다. 이는 테스트하기 쉽고 패치 파일에서 실행하여 주석이 아닌 줄이 무엇인지 확인해야 합니다.

#!/bin/sh
# delete the +++ and --- lines from unified diff. remove lines which are not
# added/removed. remove added/removed lines which are comments or blank
# output anything that is left
sed '/^+++/d;/^---/d;/^[+-]/!d;/^.[ \t]*\/\//d;/^.[ \t]*$/d' "$@"

그런 다음 함께 묶으세요

#!/bin/bash

find my/patch/folder -type f
-print0 | xargs -0 sh -c '
for i
do
  [ -z "$(./noncomments.sh "$i")" ] || cat "$i"
done
' > small.patch

리디렉션은 한 번만 수행됩니다.

작업의 균형을 약간 변경할 수 있습니다. 예를 들어 주석만 변경된 경우 스크립트가 아무것도 출력하지 않도록 하거나 주석이 변경되지 않은 경우 스크립트가 전체 파일을 출력하도록 할 수 있습니다. 그런 다음 명령은

find my/patch/folder -type f -exec ./noncommentchanges {} \; > small.patch

답변2

이러한 시나리오는 기본적으로 다음과 같은 주장의 집합입니다.자식차등 데이터. 그리고 우리는 주장을 들을 때마다 다음과 같이 연락합니다.주위를 둘러보세요, 예를 들어, 그들은진주정규식 엔진.

find my/patch/folder -type f \
-exec perl -lne '
  tr/ \t//dr =~
  m{(?=^[-+])(?!^([-+])\1{2})(?=^.(?!//))}
    and exit 0}{exit 1
' {} \; -exec cat {} + > small.patch

정규식을 연구하세요:

# delete indiscriminately all spaces n TABs  
tr/ \t//dr =~
m{ (?# From where I stand...)
  (?=^[-+])   (?# I can see the beginning of line to my immediate eight followed by either a plus or minus sign)
    ### AND ###
  (?!([-+])\1{2}) (?# I donot see --- or +++ string at the beginning of line)
    ###:AND ###
  (?=.(?!//))  (# I donot see // as the 2nd and 3rd characters)
}x;
## so when all 3 assertions are met,
can we say that we have seen a 
noncomment commit line and so we 
promptly exit with a Unixy success (0)
 else we wait till the eof to exit with a 
Unixy failure (1)

관련 정보