다른 줄에있는 파일의 여러 문자열에 대한 grep (예 : 줄 기반 검색이 아닌 전체 파일)?
나는 파일이 단어를 포함 grep으로 할 Dansk
, Svenska
또는 Norsk
가능한 반환 코드로, 어떤 라인 (문자열이 포함되어 있다는 정보를 가지고 정말 단지 등 내 한 줄이 더 다음이를 조금 간다).
다음과 같은 줄이있는 많은 파일이 있습니다.
Disc Title: unknown
Title: 01, Length: 01:33:37.000 Chapters: 33, Cells: 31, Audio streams: 04, Subpictures: 20
Subtitle: 01, Language: ar - Arabic, Content: Undefined, Stream id: 0x20,
Subtitle: 02, Language: bg - Bulgarian, Content: Undefined, Stream id: 0x21,
Subtitle: 03, Language: cs - Czech, Content: Undefined, Stream id: 0x22,
Subtitle: 04, Language: da - Dansk, Content: Undefined, Stream id: 0x23,
Subtitle: 05, Language: de - Deutsch, Content: Undefined, Stream id: 0x24,
(...)
내가 원하는 의사 코드는 다음과 같습니다.
for all files in directory;
if file contains "Dansk" AND "Norsk" AND "Svenska" then
then echo the filename
end
이를 수행하는 가장 좋은 방법은 무엇입니까? 한 줄로 할 수 있습니까?
당신이 사용할 수있는:
grep -l Dansk * | xargs grep -l Norsk | xargs grep -l Svenska
숨겨진 파일에서도 찾으려면 :
grep -l Dansk .* | xargs grep -l Norsk | xargs grep -l Svenska
bash와 grep을 사용하는 또 다른 방법 :
단일 파일 'test.txt'의 경우 :
grep -q Dansk test.txt && grep -q Norsk test.txt && grep -l Svenska test.txt
test.txt
파일에 세 가지가 모두 포함되어 있으면 인쇄 합니다 (조합). 처음 두 개의 greps는 아무것도 인쇄하지 않으며 ( -q
) 마지막 두 개의 greps는 다른 두 가지가 통과 한 경우에만 파일을 인쇄합니다.
디렉토리의 모든 파일에 대해 수행하려면 다음을 수행하십시오.
f in *; do grep -q Dansk $ f && grep -q Norsk $ f && grep -l Svenska $ f; 끝난
grep –irl word1 * | grep –il word2 `cat -` | grep –il word3 `cat -`
-i
검색 대소 문자를 구분하지 않습니다.-r
폴더를 통해 파일 검색을 재귀 적으로 만듭니다.-l
찾은 단어로 파일 목록을 파이프합니다.cat -
다음 grep이 목록에 전달 된 파일을 살펴 보도록합니다.
다른 줄에있는 파일의 여러 문자열을 grep하는 방법 (파이프 기호 사용) :
for file in *;do
test $(grep -E 'Dansk|Norsk|Svenska' $file | wc -l) -ge 3 && echo $file
done
메모:
""
grep에 큰 따옴표를 사용 하면\|
Dansk, Norsk 및 Svenska를 검색하려면 다음과 같이 파이프를 이스케이프해야합니다 .한 줄에 하나의 언어 만 있다고 가정합니다.
연습 : http://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/
여러 파일에서 여러 단어를 검색합니다.
egrep 'abc|xyz' file1 file2 ..filen
ack를 사용 하면 정말 쉽게 할 수 있습니다 .
ack -l 'cats' | ack -xl 'dogs'
-l
: 파일 목록 반환-x
: STDIN (이전 검색)에서 파일을 가져와 해당 파일 만 검색합니다.
그리고 원하는 파일을 얻을 때까지 계속 파이핑 할 수 있습니다.
awk '/Dansk/{a=1}/Norsk/{b=1}/Svenska/{c=1}END{ if (a && b && c) print "0" }'
그런 다음 쉘로 반환 값을 잡을 수 있습니다.
Ruby (1.9+)가있는 경우
ruby -0777 -ne 'print if /Dansk/ and /Norsk/ and /Svenka/' file
간단히:
grep 'word1\|word2\|word3' *
자세한 내용은 이 게시물 을 참조하십시오.
이것은 glenn jackman과 kurumi의 답변을 혼합하여 임의의 수의 고정 단어 또는 고정 된 정규식 집합 대신 임의의 수의 정규식을 허용합니다.
#!/usr/bin/awk -f
# by Dennis Williamson - 2011-01-25
BEGIN {
for (i=ARGC-2; i>=1; i--) {
patterns[ARGV[i]] = 0;
delete ARGV[i];
}
}
{
for (p in patterns)
if ($0 ~ p)
matches[p] = 1
# print # the matching line could be printed
}
END {
for (p in patterns) {
if (matches[p] != 1)
exit 1
}
}
다음과 같이 실행하십시오.
./multigrep.awk Dansk Norsk Svenska 'Language: .. - A.*c' dvdfile.dat
@kurumi의 awk 대답을 확장하면 다음과 같은 bash 함수가 있습니다.
all_word_search() {
gawk '
BEGIN {
for (i=ARGC-2; i>=1; i--) {
search_terms[ARGV[i]] = 0;
ARGV[i] = ARGV[i+1];
delete ARGV[i+1];
}
}
{
for (i=1;i<=NF; i++)
if ($i in search_terms)
search_terms[$1] = 1
}
END {
for (word in search_terms)
if (search_terms[word] == 0)
exit 1
}
' "$@"
return $?
}
용법:
if all_word_search Dansk Norsk Svenska filename; then
echo "all words found"
else
echo "not all words found"
fi
저에게 잘 맞는 것은 다음과 같습니다.
find . -path '*/.svn' -prune -o -type f -exec gawk '/Dansk/{a=1}/Norsk/{b=1}/Svenska/{c=1}END{ if (a && b && c) print FILENAME }' {} \;
./path/to/file1.sh
./another/path/to/file2.txt
./blah/foo.php
If I just wanted to find .sh files with these three, then I could have used:
find . -path '*/.svn' -prune -o -type f -name "*.sh" -exec gawk '/Dansk/{a=1}/Norsk/{b=1}/Svenska/{c=1}END{ if (a && b && c) print FILENAME }' {} \;
./path/to/file1.sh
I did that with two steps. Make a list of csv files in one file With a help of this page comments I made two scriptless steps to get what I needed. Just type into terminal:
$ find /csv/file/dir -name '*.csv' > csv_list.txt
$ grep -q Svenska `cat csv_list.txt` && grep -q Norsk `cat csv_list.txt` && grep -l Dansk `cat csv_list.txt`
it did exactly what I needed - print file names containing all three words.
Also mind the symbols like `' "
If you only need two search terms, arguably the most readable approach is to run each search and intersect the results:
comm -12 <(grep -rl word1 . | sort) <(grep -rl word2 . | sort)
I had this problem today, and all one-liners here failed to me because the files contained spaces in the names.
This is what I came up with that worked:
grep -ril <WORD1> | sed 's/.*/"&"/' | xargs grep -il <WORD2>
'program tip' 카테고리의 다른 글
강제로 복사하는 방법이 있습니까? (0) | 2020.10.12 |
---|---|
람다를 키에 바인딩 할 때 "잘못된 형식 인수 : commandp"오류 (0) | 2020.10.12 |
스크립트에서 stdout을 캡처 하시겠습니까? (0) | 2020.10.12 |
추천하는 Javascript 템플릿 엔진은 무엇입니까? (0) | 2020.10.12 |
포인터 주소와 포인터 값을 증가시키는 방법은 무엇입니까? (0) | 2020.10.12 |