sed를 사용하여 빈 파일을 건너뛰는 방법은 무엇입니까?

sed를 사용하여 빈 파일을 건너뛰는 방법은 무엇입니까?

나는 sed이것을 다음과 같이 사용하고 있습니다 :

 sed -e 's/ *| */|/g'
   ${array_export_files[$loopcount]}>>$TEMPDIR/"export_file"_${testid}_${loopcount}_$$

while 루프에서는 파일이 비어 있거나 내용이 없으면 문제가 발생합니다.

  1. sed파일이 존재하지만 비어 있으면 실행하고 싶지 않습니다.
  2. sed파일이 존재하지 않으면 실행하고 싶지 않습니다.

전체 코드 조각은 다음과 같습니다.

while [ $loopcount -le $loopmax ]
do 
    if [ "$loopcount" -eq "$loopcount" ]
    then
        sed -e 's/ *| */|/g' ${array_export_files[$loopcount]}>>$TEMPDIR/"export_file"_${testid}_${loopcount}_$$
        tr "|" "\t" <"export_file"_${testid}_${loopcount}_$$>${array_export_files[$loopcount]}
        cp ${array_export_files[$loopcount]} "export_file"_${loopcount}_${testid}
        echo "Testing Starts Here"
        echo ${array_export_files[$loopcount]} "export_file"_${loopcount}_${testid}
        echo "Testing Ends Here"
    fi
  (( loopcount=`expr $loopcount+1`))
done    

따라서 위의 if 문에서 AND 연산자를 바꾸거나 사용할 수 없습니다. 이 문제를 해결할 수 있는 방법이 있습니까? AND 연산자를 사용하면 아래의 전체 코드 부분을 건너뛸 수 있으며 실행되지 않습니다. 조건부로 sed 부분을 건너뛰고 싶습니다.

답변1

-sBash에는 존재 여부를 테스트할 수 있는 옵션이 있습니다.그리고크기가 0보다 큽니다:

 -s file
          True if file exists and has a size greater than zero.

그래서 당신은 할 수 있습니다

if [ -s "${array_export_files[$loopcount]}" ]; then
   sed .......
fi

루프 내에서. 이는 항상 사실 이므로 if [ "$loopcount" -eq "$loopcount" ]다음과 같이 바꿀 수 있습니다.

while [ "$loopcount" -le "$loopmax" ]
do 
    if [ -s "${array_export_files[$loopcount]}" ]
    then
        sed -e 's/ *| */|/g' "${array_export_files[$loopcount]}" >>" $TEMPDIR/export_file_${testid}_${loopcount}_$$"
        tr "|" "\t" <"export_file_${testid}_${loopcount}_$$">"${array_export_files[$loopcount]}"
        cp "${array_export_files[$loopcount]}" "export_file_${loopcount}_${testid}"
        echo "Testing Starts Here"
        echo "${array_export_files[$loopcount]}" "export_file_${loopcount}_${testid}"
        echo "Testing Ends Here"
    fi
    (( loopcount = loopcount + 1 ))
done

관련 정보