新しいファイルが別のファイルに表示されたら、どのように新しいファイルに書き込むことができますか?

新しいファイルが別のファイルに表示されたら、どのように新しいファイルに書き込むことができますか?

他のファイルで見つかったイベントに基づいて新しいファイルを作成して書き込む必要があります。つまり:

Occurrence found in first file
then write same Occurrence in another one/new

詳細な詳細:

「File1」:発生回数を検索します。

Occurrence1
Occurrence2
OccurrenceN

##If the `Occurence1` is find in `File1` then write in the `new file` the same Occurrence

kshファイルの発生回数と発生しない回数を指定する次の関数コマンドがあります。

users=(Occurrence1 Occurrence2 Occurrence3 Occurrence4 ... OccurrenceN)
for i in "${users[@]}"
do
grep -qw $i file1 && echo "$i is in the file" || echo "$i is not in the file"
done

前のコードをいくつか修正しました。

users=(Occurrence1 Occurrence2 Occurrence3 ... OccurrenceN)
for i in "${users[@]}"
do
        grep -qw $i File1.txt && echo "$i is in the file" || echo "$i is not in the file"
       if [[ $user = "*is in the file" ]]; then
       echo $user >> users_in_file.txt
       elif [[ $user = "*is not in the file" ]]; then
       echo $user >> users_not_in_file.txt
       fi
done

目標を達成するために最後のコマンドを実行するという考えがありますが、うまくいきません。私ができることはもうありますか?よろしくお願いします。質問がある場合はコメントを残してください。

ベストアンサー1

grep条件は次のように直接使用できますif

users=(Occurrence1 Occurrence2 Occurrence13  OccurrenceN)
for i in "${users[@]}"
do
       if grep -qw "$i" File1.txt; then
                echo "$i is in the file"
                echo "$i" >> users_in_file.txt
       else
                echo "$i is not in the file"
                echo "$i" >> users_not_in_file.txt
       fi
done

おすすめ記事