grep -v , -E 옵션을 이용한 문자열 제외하여 찾기

오늘은 terminal에서 데이터를 걸러낼 때 많이 사용하시는 grep에 대한 이야기를 할까 합니다. 특별한건 아니고 -v(invert)와 -E(regex)를 이용해서 원하는 패턴의 값만 걸러내는 방법입니다.

사용법: grep [옵션]... 패턴 [파일]...
...snip...

정규식 선택과 해석
  -E, --extended-regexp     확장된 정규 표현식으로 된 패턴 (ERE)

Miscellaneous:
  -s, --no-messages         suppress error messages
  -v, --invert-match        select non-matching lines
  -V, --version             display version information and exit
      --help                display this help text and exit

-v는 invert-match를 의미하고, -E는 regex, 즉 정규표현식을 의미하는 flag입니다. 이 2개를 조합하면 원하는 패턴의 값만 쉽게 걸러낼 수 있습니다.

-rwsrwxrwx  1 root root    0  9월 15 17:23 4777_abc
-rwsrwxrwx  1 root root    0  9월 15 17:23 4777_cba
-rwsrwxrwx  1 root root    0  9월 15 17:30 4777_zzza
-rwsrwxrwx  1 root root    0  9월 15 17:30 4777_zzzc
-rwsrwxrwx  1 root root    0  9월 15 17:30 4777_zzze
-rwsrwxrwx  1 root root    0  9월 15 17:30 4777_zzzh
-rwsrwxrwx  1 root root    0  9월 15 17:30 4777_zzzq
-rwsrwxrwx  1 root root    0  9월 15 17:30 4777_zzzs
-rwsrwxrwx  1 root root    0  9월 15 17:30 4777_zzzz

여기서 -E(정규식) 옵션을 이용해 abc와 zzzz, zzzh 문자열이 제외된 값만 검색합니다.

find ./* -perm 4777 | grep -Ev 'abc|zzzh|zzzz'
./4777_cba
./4777_zzza
./4777_zzzc
./4777_zzze
./4777_zzzq
./4777_zzzs