목록을 검색하는 순서대로 다른 파일의 목록에서 문자열이 포함된 텍스트 파일의 줄을 추출하는 방법은 무엇입니까?

목록을 검색하는 순서대로 다른 파일의 목록에서 문자열이 포함된 텍스트 파일의 줄을 추출하는 방법은 무엇입니까?

파일 1: 소스 file.txt

Hello, It's the beginning of the sentence. 
it is the beginpoint of my career.
The end is always far.
We can start our beginpoint anytime we want.
The time we utilise to make our life good should be more.
This text doesn't mean anything.
I am writing this to include my three points:
beginpoint
time
end

파일 2: string.txt

beginpoint
end
time

원하는 출력:

it is the beginpoint of my career
We can start our beginpoint anytime we want.
beginpoint
The end is always far.
end
The time we utilise to make our life good should be more.
time

나는 사용했다

grep -w -F -f  strings.txt sorcefile.txt > outputfile.txt

나는 출력을 얻습니다 :

it is the beginpoint of my career.
The end is always far.
We can start our beginpoint anytime we want.
The time we utilise to make our life good should be more.
beginpoint
time
end

따라서 행은 원하는 대로이지만 소스 파일과 동일한 순서가 아닌 검색어 순서로 그룹화하고 싶습니다.

답변1

grep한 가지 방법은 한 줄에 한 번씩 호출하는 것입니다.strings.txt

$ while IFS= read -r line; do grep -wF "$line" sourcefile.txt; done < strings.txt
it is the beginpoint of my career.
We can start our beginpoint anytime we want.
beginpoint
The end is always far.
end
The time we utilise to make our life good should be more.
time

strings.txt파일이 너무 길면 속도가 느려질 수 있습니다 . 쉘 루프를 사용하여 텍스트를 처리하는 것이 왜 나쁜 습관으로 간주됩니까?


sed플래그를 지원하는 경우 e:

$ sed 's/.*/grep -wF '"'&'"' sourcefile.txt/e' strings.txt
it is the beginpoint of my career.
We can start our beginpoint anytime we want.
beginpoint
The end is always far.
end
The time we utilise to make our life good should be more.
time

답변2

귀하의 예와 같이 문자열 목록에 공백이 포함되어 있지 않다고 가정합니다.

$ awk -F'[^[:alnum:]_]+' '
    NR==FNR { strs[$0]; next }
    { for (str in strs) for (i=1; i<=NF; i++) if ($i==str) print str, FNR, $0 }
' file2 file1 | sort -k1,1 -k2,2n | cut -d' ' -f3-
it is the beginpoint of my career.
We can start our beginpoint anytime we want.
beginpoint
The end is always far.
end
The time we utilise to make our life good should be more.
time

위의 작업은 일치하는 문자열을 포함하는 줄을 인쇄할 뿐만 아니라 일치하는 문자열과 일치하는 줄 번호를 인쇄하는 방식으로 작동합니다(정렬 후 상대 순서를 유지하기 위해 - GNU 정렬을 사용하는 경우에는 필요하지 않음 -s). 그런 다음 추가된 장식을 삭제합니다. 첫 번째 단계. 이것은 단계별로:

$ awk -F'[^[:alnum:]_]+' 'NR==FNR{strs[$0];next} {for (str in strs) for (i=1; i<=NF; i++) if ($i==str) print str, FNR, $0}' file2 file1
beginpoint 2 it is the beginpoint of my career.
end 3 The end is always far.
beginpoint 4 We can start our beginpoint anytime we want.
time 5 The time we utilise to make our life good should be more.
beginpoint 8 beginpoint
time 9 time
end 10 end

.

$ awk -F'[^[:alnum:]_]+' 'NR==FNR{strs[$0];next} {for (str in strs) for (i=1; i<=NF; i++) if ($i==str) print str, FNR, $0}' file2 file1 | sort -k1,1 -k2,2n
beginpoint 2 it is the beginpoint of my career.
beginpoint 4 We can start our beginpoint anytime we want.
beginpoint 8 beginpoint
end 3 The end is always far.
end 10 end
time 5 The time we utilise to make our life good should be more.
time 9 time

.

$ awk -F'[^[:alnum:]_]+' 'NR==FNR{strs[$0];next} {for (str in strs) for (i=1; i<=NF; i++) if ($i==str) print str, FNR, $0}' file2 file1 |
    sort -k1,1 -k2,2n | cut -d' ' -f3-
it is the beginpoint of my career.
We can start our beginpoint anytime we want.
beginpoint
The end is always far.
end
The time we utilise to make our life good should be more.
time

관련 정보