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

おすすめ記事