1

I have a python script that basically does some stuff on a file and results in a shorter file. I want the script to continue running on each subsequent output file until the output file and the most recent input file have the same number of lines.

The python script works like this:

python Joiner.py input.txt output.txt

I want to create a BASH script that will run something like this:

while [outputfilelines -lt inputfilelines]
do

python Joiner.py inputfile.txt outputfile.txt

done

Where each output file then becomes the input file for the next loop. I've tried a few different things but I can't seem to get it to work correctly. I can't figure out how to keep one file in the loop and spit out the other one.

Any help would be greatly appreciated.

Thanks.

4

3 に答える 3

1
IN=input.txt
OUT=output.txt

touch $OUT
while [ ! $(wc -l < $IN) -eq $(wc -l < $OUT) ]; do
        python Joiner.py $IN $OUT
        mv $OUT $IN
done
于 2013-08-27T04:13:51.830 に答える
0

これはかなり力ずくで無知です。

I_FILE="input.txt"
O_FILE="output.txt"

old=$(wc -l <"$I_FILE")
while python Joiner.py "$I_FILE" "$O_FILE"
      new=$(wc -l < "$O_FILE")
      mv "$O_FILE" "$I_FILE"
      [ $new != $old ]
do
    old=$new
done

while条件は、pythonコマンド、new=行数の割り当て、移動コマンド、および[ $new != $old ]ループを制御するテスト (カウントされるのはテスト) の4つのコマンドを評価します。カウントが異なる場合は、新しいカウントを古いカウントとして記録し、繰り返します。whileシェルループの条件部分で、このような複数のコマンドを実行できることを知っている人は多くありません。

于 2013-08-27T04:15:12.253 に答える
0

内容も同じになる場合は、diff を使用できます。

while ! diff "$IN" "$OUT"; do
    python Joiner.py "$IN" "$OUT"
    mv "$OUT" "$IN"
done
于 2013-08-27T04:18:39.940 に答える