새 파일이 다른 파일에 나타나면 어떻게 새 파일에 쓸 수 있나요?

새 파일이 다른 파일에 나타나면 어떻게 새 파일에 쓸 수 있나요?

다른 파일에서 발견된 이벤트를 기반으로 새 파일을 만들고 써야 합니다. 즉:

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

관련 정보