![존재하지 않는 경우 한 파일에서 다른 파일의 문자열을 찾아 원본 파일에서 삭제합니다.](https://linux55.com/image/88861/%EC%A1%B4%EC%9E%AC%ED%95%98%EC%A7%80%20%EC%95%8A%EB%8A%94%20%EA%B2%BD%EC%9A%B0%20%ED%95%9C%20%ED%8C%8C%EC%9D%BC%EC%97%90%EC%84%9C%20%EB%8B%A4%EB%A5%B8%20%ED%8C%8C%EC%9D%BC%EC%9D%98%20%EB%AC%B8%EC%9E%90%EC%97%B4%EC%9D%84%20%EC%B0%BE%EC%95%84%20%EC%9B%90%EB%B3%B8%20%ED%8C%8C%EC%9D%BC%EC%97%90%EC%84%9C%20%EC%82%AD%EC%A0%9C%ED%95%A9%EB%8B%88%EB%8B%A4..png)
파일의 각 줄을 살펴보고 다른 텍스트 파일의 어떤 줄에서도 줄이 일치하지 않으면 원본 파일에서 해당 줄을 제거하는 스크립트를 만들려고 합니다.
이 스크립트에 필요한 입력 및 출력의 예는 다음과 같습니다.
입력 예: 파일 1(그룹 파일),
hello
hi hello
hi
great
interesting
file 2:
this is a hi you see
this is great don't ya think
sometimes hello is a good expansion of its more commonly used shortening hi
interesting how brilliant coding can be just wish i could get the hang of it
샘플 스크립트 출력 - 파일 1이 다음으로 변경됩니다.
hello
hi
great
interesting
hi hello
그래서 두 번째 파일에는 존재하지 않아서 삭제되었습니다.
여기에 스크립트가 있습니다. 변수를 생성하는 지점까지 작동하는 것 같습니다.
#take first line from stability.contigs.groups
echo | head -n1 ~/test_folder/stability.contigs.groups > ~/test_folder/ErrorFix.txt
#remove the last 5 character
sed -i -r '$ s/.{5}$//' ~/test_folder/ErrorFix.txt
#find match of the word string in errorfix.txt in stability.trim.contigs.fasta if not found then delete the line containing the string in stability.contigs.groups
STRING=$(cat ~/test_folder/MothurErrorFix.txt)
FILE=~/test_folder/stability.trim.contigs.fasta
if [ ! -z $(grep "$STRING" "$FILE") ]
then
perl -e 's/.*\$VAR\s*\n//' ~/test_folder/stability.contigs.groups
fi
답변1
가지고 있다면 gnu grep
다음을 실행할 수 있습니다.
grep -oFf file1 file2 | sort | uniq | grep -Ff - file1
grep
중간 행의 순서를 유지할 필요가 없으면 마지막 행을 삭제하세요 file1
.
액세스 권한이 없으면 gnu grep
다음을 수행하십시오 awk
.
awk 'NR==FNR{z[$0]++;next};{for (l in z){if (index($0, l)) y[l]++}}
END{for (i in y) print i}' file1 file2
답변2
그렇다면 don_crissti의 (승인된) 답변을 문의하세요 GNU grep
. 그렇지 않은 경우(예: 표준 Mac OS X에서는 작동하지 않음) 이 코드 조각을 bash 스크립트에 저장할 수도 있습니다.myconvert.sh
#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
if ! grep -Fq "$line" $2
then
sed -i '' "/$(echo $line | sed -e 's/[]\/$*.^|[]/\\&/g')/d" $1
fi
done < "$1"
두 개의 파일을 인수로 사용하여 호출하세요.
./myconvert.sh file1 file2
그러나 while/read 사용 및 호출의 명백한 성능 단점에 관한 아래 don_crissti의 전문가 의견을 참고하세요 sed
.