1

for ループで実行するには grep -f のヘルプが必要

基本的に、name.txt の各エントリについて、A.txt からすべての行を grep し、別のファイルに書き出す必要があります。

例 1) name.txt は、次の 3 つの名前のリストです。

America
Europe   
Asia

2) A.txtは(タブ区切り)

X y Z America
x a b Asia
y b c America
a b c Europe
x y z Europe
a b c America

name.txt ファイルから各エントリを取得し、A.txt で対応する行を検索し、3 つの個別の出力ファイルで返します。

file1: X y Z America
       y b c America
       a b c America

file2: a b c Europe
       x y z Europe
file3: x a b Asia

スクリプトで書いてbashで実行している可能性がありますか?

前もって感謝します!!!

4

2 に答える 2

2

次のスクリプトを (./script.sh name.txt input.txt として) 実行します。ここで、name.txt には名前があり、input.txt は入力ファイルです。出力ファイルは、file_America.txt、file_Asia.txt、および file_Europe.txt として保存されます。

#!/bin/bash -x

while read line; do
#skip empty line
[ -z "$line" ] && continue;
#run grep and save the output
grep "$line" "$2" > file_$line.txt;
done < "$1"
于 2012-08-10T18:42:09.157 に答える
0

を使用した片道awk

awk '
    ## Process first file of arguments. Save data in an array, the name as
    ## key, and the number of the output file to write as value.
    FNR == NR {
        name[ $1 ] = FNR;
        next;
    }

    ## Process second file of arguments. Check last field of the line in the 
    ## array and print to file matched by the value.
    FNR < NR {
        print $0 > sprintf( "%s%d", "file", name[ $NF ] );
    }
' name.txt A.txt

出力ファイルを確認します。

head file[123]

次の結果で:

==> file1 <==
X y Z America
y b c America
a b c America

==> file2 <==
a b c Europe
x y z Europe

==> file3 <==
x a b Asia
于 2012-08-10T19:38:55.970 に答える