리눅스 grep 명령


  번역 결과:

grep

English ['grep] US ['grep]

[계산] 타겟 라인 검색 명령

리눅스 grep 명령통사론

기능: grep 명령은 파일에서 조건을 충족하는 문자열을 찾는 데 사용됩니다.

구문: ​​grep [-abcEFGhHilLnqrsvVwxy][-A<열 번호 표시>][-B<열 번호 표시>][-C<작업 수행>][-d<작업 수행>] [-e< 템플릿 스타일>][-f<템플릿 파일>][--help][템플릿 스타일][파일 또는 디렉터리...]

리눅스 grep 명령예

1. 현재 디렉터리에서 file이라는 단어가 붙은 파일에서 테스트 문자열이 포함된 파일을 찾아 해당 문자열의 줄을 인쇄합니다. 이때 다음 명령어를 사용할 수 있습니다.

grep test *file

결과는 다음과 같습니다.

$ grep test test* #查找前缀有“test”的文件包含“test”字符串的文件  
testfile1:This a Linux testfile! #列出testfile1 文件中包含test字符的行  
testfile_2:This is a linux testfile! #列出testfile_2 文件中包含test字符的行  
testfile_2:Linux test #列出testfile_2 文件中包含test字符的行

2. 조건에 맞는 파일을 재귀적으로 찾습니다. 예를 들어, 지정된 /etc/acpi 디렉토리와 그 하위 디렉토리(하위 디렉토리가 존재하는 경우)의 모든 파일에서 "update"라는 문자열이 포함된 파일을 찾고 해당 문자열이 있는 줄의 내용을 인쇄하려면 다음 명령을 사용합니다. :

grep -r update /etc/acpi

출력 결과는 다음과 같습니다.

$ grep -r update /etc/acpi #以递归的方式查找“etc/acpi”  #下包含“update”的文件  
/etc/acpi/ac.d/85-anacron.sh:# (Things like the slocate updatedb cause a lot of IO.)  Rather than  
/etc/acpi/resume.d/85-anacron.sh:# (Things like the slocate updatedb cause a lot of  
IO.) Rather than  
/etc/acpi/events/thinkpad-cmos:action=/usr/sbin/thinkpad-keys--update

3. 이전 예제는 조건에 맞는 라인을 찾아 출력하는 것이었습니다. "-v" 매개변수를 사용하면 조건에 맞지 않는 라인의 내용을 출력할 수 있습니다.

파일 이름에 test가 포함된 파일에서 test가 포함되지 않은 줄을 찾으세요. 이때 사용된 명령은

grep -v test *test*

결과는 다음과 같습니다.

$ grep-v test* #查找文件名中包含test 的文件中不包含test 的行  
testfile1:helLinux!  
testfile1:Linis a free Unix-type operating system.  
testfile1:Lin  
testfile_1:HELLO LINUX!  
testfile_1:LINUX IS A FREE UNIX-TYPE OPTERATING SYSTEM.  
testfile_1:THIS IS A LINUX TESTFILE!  
testfile_2:HELLO LINUX!  
testfile_2:Linux is a free unix-type opterating system.

인기 추천

비디오

Q&A