Linux2010. 3. 17. 16:24
grep --color=auto 를 하면 자동으로 색상을 넣어주는데
검색 키워드를 "빨간색"으로 넣어주는 기능이 좀 마음에 든다.
그런데 가끔, 검색 내용뿐만 아니라 전체 내용에서 그 부분만 색상을 바꾸고 싶다면 어떻게 하면 되려나?
(일종의 터미널용 문법강조/신택스 하이라이트/syntax highlight)

grep 명령어에는 Before & After를 지원한다.
       -A NUM, --after-context=NUM
              Print NUM  lines  of  trailing  context  after  matching  lines.
              Places  a  line  containing  --  between  contiguous  groups  of
              matches.

       -B NUM, --before-context=NUM
              Print  NUM  lines  of  leading  context  before  matching lines.
              Places  a  line  containing  --  between  contiguous  groups  of
              matches.

[링크 : http://linux.die.net/man/1/grep]

검색어의 앞뒤로 조금더 출력해주느 기능인데, 검색어들 간에 거리가 적당하게 유지된다면
특정값을 넣어주면, 전체 문서에서 그 부분만 색상이 변해서 나오게 된다.

문서의 전체 길이를 재려면
"wc -l" 로 하면되고
       -l, --lines
              print the newline counts

[링크 : http://linux.die.net/man/1/wc]

파일이 아닐경우 pipe를 통해 넘겨주면 된다.

예를들어 grep의 manpage 에서 it만 강조하고 싶다면
# man grep | grep -A `man grep | wc -l` -B `man grep | wc -l` it
라고 입력하면 된다.

아래는 it의 빈도가 낮아서 a로 바꾸어서 검색해본 결과.
a에만 색상이 나오고 터미널 스크롤바는 길어지고 ㅋㅋ

음.. 깔끔한 방법은 없나?

Posted by 구차니

댓글을 달아 주세요

일반적으로 Ctrl-R 옵션이나 Ctrl-H 옵션등으로
디렉토리 내의 모든 파일의 내용을 치환하는 것이 있다.

find ./ -exec grep "content" {} \; -exec sed -i "s/content/container/g" {} \;

[링크 : http://www.linux-forum.de/find-grep-replace-string-33557.html]

아무튼 위의 경우는 grep에 옵션이 들어가지 않았고, 모든 파일에 대해서 검색을 하므로
ctags 사용중에는 tags 파일까지 검색을 하게 된다.

크로스컴파일 목적으로 CC 라던가 LD 등을 고치려고 하면

find ./ -name Makefile -exec grep -w "= gcc" {} \; -exec sed -i "s/\= gcc/\= sh4-linux-gcc/g" {} \;

이렇게 find에 -name 옵션으로 파일을 지정하고(makefile 혹은 Makefile)
grep 옵션에 -w (Whole word matching) 으로 한정을 해주면 된다.

2010.02.04 추가
위의 예제는 "= gcc" 라고 들어있는 부분을 전체가 포함된 내용을 찾아 "= sh4-linux-gcc"로 변경해주는 것이다.
예를 들어 "= gcc3" 이런식으로 된 곳은  grep -w 옵션에 의해 무시된다.
그리고 tags 파일을 피하기 위해 find ./ -name Makefile 로 makefile에서만 수정을 하도록 제한한다.

2010/02/01 - [Linux] - grep에서 정확하게 찾기

Posted by 구차니

댓글을 달아 주세요

Linux2010. 2. 1. 15:30

메모장이나 각종 텍스트 에디터에서 지원하는
"완전히 일치하는 단어" 검색은 grep 에서 -w 옵션을 사용하면 된다.

-w, --word-regexp
    Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.

[링크 : http://linux.die.net/man/1/grep]

[링크 : http://www.unix.com/unix-dummies-questions-answers/42144-find-exactly-word-grep-command.html]
Posted by 구차니

댓글을 달아 주세요

Linux2009. 9. 2. 17:59
$ man grep
       -v, --invert-match
              Invert the sense of matching, to select non-matching lines.

-v 옵션은 invert match로 문장을 포함하지 않는 줄을 출력해준다.


사용예 : 주석이 아닌 줄만 찾기
Posted by 구차니

댓글을 달아 주세요

Linux2009. 8. 6. 10:13
저번에 두번 정도 시스템 말아 먹은 강력한 녀석인데..
버그인지 모르겠지만

grep -rnI "keyworld" ./ > log.find

이런식으로 검색을 하면, 검색이 끝나지 않고 파일이 무한대로 커지는 문제가 있다.
저렇게 언제 끝나나 냅두고 있으면... 시스템 뻗으니 주의!!!
Posted by 구차니
TAG grep

댓글을 달아 주세요

Linux2009. 7. 21. 14:19
ctrl-shift-f 로 디렉토리 검색하거나 하는 것과 유사하게
grep을 이용하는데
주로 사용하는 옵션은 rni이다.

NAME
       grep, egrep, fgrep - print lines matching a pattern

SYNOPSIS
       grep [options] PATTERN [FILE...]
       grep [options] [-e PATTERN | -f FILE] [FILE...]

       -I     Process  a  binary  file as if it did not contain matching data;
                this is equivalent to the --binary-files=without-match option.

       -i, --ignore-case
              Ignore case distinctions in both the PATTERN and the input files.

       -n, --line-number
              Prefix each line of output with the line number within its input file.

       -R, -r, --recursive
              Read all files under each directory, recursively; this is equivalent to the -d recurse option.

grep -rniI "검색어" "검색시작 경로"

의 양식으로 사용하며
예를 들어 현재 위치 아래로 test_str 이라는 것을 검색하려면

grep -rniI test_str ./

라고 하면된다.
Posted by 구차니
TAG grep

댓글을 달아 주세요

Linux2009. 6. 9. 10:43
MAC Address
$ /sbin/ifconfig eth0 | grep HWaddr | awk '{print $5}'

IP Address
$ /sbin/ifconfig eth0 | grep 'inet addr' | awk '{print $2}'

Broadcast Address
$ /sbin/ifconfig eth0 | grep 'inet addr' | awk '{print $3}'

SUBNET mask
$ /sbin/ifconfig eth0 | grep 'inet addr' | awk '{print $4}'

GATEWAY Address
$ netstat -rn | grep ^0.0.0.0 | awk '{printf $2}'

머.. 윈도우 비스므리하게 출력하기 위해서는 Broadcast Address는 별 의미를 가지지 않을테니
나머지 4가지면 충분할 듯!

awk
[참고: http://wiki.kldp.org/wiki.php/Awk]
[참고: http://kldp.org/HOWTO/html/Adv-Bash-Scr-HOWTO/awk.html]

sed
[참고: http://kldp.org/HOWTO/html/Adv-Bash-Scr-HOWTO/x12718.html]
[참고: http://stone.backrush.com/sunfaq/ljs007.html]

'Linux' 카테고리의 다른 글

ifconfig --help  (0) 2009.06.09
dhcp 작동중인지 확인하는 방법  (0) 2009.06.09
하드디스크 정보 얻어내기(model명)  (0) 2009.06.08
하드웨어 정보 받아오기  (0) 2009.06.08
리눅스에서 hex edit 하기  (0) 2009.05.28
Posted by 구차니

댓글을 달아 주세요