0

テキストファイル(〜8 GB)があります。このファイルを A としましょう。ファイル A には、19 の単語と整数がスペースで区切られた約 100,000 行があります。ファイル A から数行を切り取り、新しいファイル (ファイル B) に貼り付ける必要があります。行はファイル A から削除する必要があります。ファイル A から切り取る行には、正確に一致する文字列が含まれている必要があります。次に、これを数回繰り返し、ファイル A から一致する文字列が毎回異なる行を削除する必要があります。毎回、ファイル A が小さくなっています。「sed」を使用してこれを行うことができますが、次のように 2 つのコマンドを使用します。

# Finding lines in file A with matching string and copying those lines to file B
sed -ne '/\<matchingString\>/ p' file A > file B

#Again finding the lines in file A with matching string and deleting those lines,
#writing a tmp file to hold the lines that were not deleted.
sed '/\<matchingString\>/d'file A > tmp

# Replacing file A with the tmp file.
mv tmp file A

ファイル A と B の例を次に示します。hg15 ファイル A を含むすべての行を抽出したい:

ID pos frac xp mf ...
23 43210 0.1 2 hg15...
...
...

File B:
23 43210 0.1 2 hg15...

私はシェル スクリプトを書き、すべての Unix ツールを使用することにかなり慣れていませんが、これをよりエレガントかつ迅速に行うことができるはずだと感じています。誰でもこのスクリプトを改善するために私を案内してくれませんか? 特に「sed」を使用する必要はありません。この正確な問題の解決策を見つけることなく、私はウェブとスタックオーバーフローを検索してきました。RedHat と bash を使用しています。ありがとう。

4

3 に答える 3

0

これがあなたを助けることを願っています...

cat File A | while read line     
do        

    #Finding lines in file A wit matching string and copying those lines to file B
    sed -ne '/\<matchingString\>/ p' file A >> file B

    #Again finding the lines in file A with matching string and deleting those lines  
    #writing a tmp file to hold the lines that were not deleted     
    sed '/\<matchingString\>/d'file A >> tmp

done

#once you are done with greping and copy pasting Replacing file A with the tmp file    
`mv tmp file A`

PS: 一致パターンが見つかったときにループ内で grep しているため、ファイル B に追加しています。

于 2013-09-26T09:22:53.640 に答える