1

多くの数値 (+2M) を含むファイル 'tbook1' があります。bash (Solaris / RHEL) で以下を実行する必要があります。

Do following:
Remove 1st and last 2 lines
Remove (,") & (")
Substitute (, ) with (,)

私は2つの方法を使用してそれを行うことができます:

Method1:
sed -e 1d -e 's/,"//g' -e 's/, /,/g' -e 's/"//g' -e 'N;$!P;$!D;$d' tbook1 > tbook1.3

method2:
tail -n +2 tbook1 | head -n -2 > tbook1.1
sed -e 's/,"//' -e 's/, //' tbook 1.1 > tbook1.2

どちらが優れているか、つまり、より高速で効率的 (リソースの使用) か知りたいですか?

4

3 に答える 3

1

方法 1 は通常、より効率的です。これは主に、方法 2 の余分なパイプと、読み書きされる中間ファイルのためです。

于 2013-03-20T07:24:11.747 に答える
1

方法 1 は、ファイルを 1 回だけスキャンして 1 つの結果を書き込みます (ただし、結果は別の名前のファイルに保存してください) 方法 2 2 は、元のファイルと中間結果をスキャンし、中間結果と最終結果を書き込みます。約2倍遅くなるはずです。

于 2013-03-20T07:25:13.197 に答える
1

私は、純粋なよりもこの行削除タスクの方が効率的だheadと思います。しかし、他の 2 つの答えも正しいです。複数のパスを実行することは避けてください。tailsed

2 番目の方法は、それらを連鎖させることで改善できます。

tail -n +2 book.txt | head -n -2 | sed -e 's/,"//' -e 's/, //'

次にhead、およびtailの方が高速です。自分で試してみてください(妥当なサイズのファイルで):

#!/usr/bin/env bash

target=/dev/null

test(){
        mode=$1
        start=$(date +%s)
        if   [ $mode == 1 ]; then
                sed -e 1d -e 's/,"//g' -e 's/, /,/g' -e 's/"//g' -e 'N;$!P;$!D;$d' book.txt > $target
        elif [ $mode == 2 ]; then
                tail -n +2 book.txt | head -n -2 | sed -e 's/,"//' -e 's/, //' > $target
        else
                cat book.txt > /dev/null
        fi

        ((time = $(date +%s) - $start))
        echo $time "seconds"
}

echo "cat > /dev/null"
test 0

echo "sed > $target"
test 1

echo "tail/head > $target"
test 2

私の結果:

cat > /dev/null
0 seconds

sed > /dev/null
5 seconds

tail/head > /dev/null
3 seconds
于 2013-03-20T07:59:04.697 に答える