현재 각 파일에 최대 1 줄의 매우 긴 데이터가있는 디렉토리에 n 개의 데이터 파일이 있습니다. 내 디렉토리 구조는
director/
data1.json
data2.json
data3.json
해당 파일 중 적어도 하나에 내가 찾고있는 키워드가 포함되어 있지만 한 줄의 데이터가 너무 길어서 전체 터미널을 포함합니다. 키워드 grep을 수행 한 후에 만 파일 이름을 얻는 방법은 무엇입니까? 내가 사용하는 grep 명령은 다음과 같습니다.
grep keyword *.json
답변
-l 인수는 원하는 것을 수행해야합니다.
-l, --files-with-matches
Suppress normal output; instead print the name of each input
file from which output would normally have been printed. The
scanning will stop on the first match. (-l is specified by
POSIX.)
답변
xargs를 grep과 함께 –null 또는 ack와 함께 –print0과 함께 사용할 수 있습니다. ack 속도가 빠르다는 것을 알았습니다.
grep -lr --null searchterm * | xargs -I {} -0 echo {}
ack -lr --print0 searchterm * | xargs -I {} -0 echo {}