grep

2dean·2023년 6월 19일
0

Linux 기초

목록 보기
9/13

grep

find나 locate같은 명령어의 파일 이름만이 아니고, 내부 정보를 포함합니다
파일의 내부를 검색하고 찾으려는 내용을 출력함 일치하는 행을 출력해줌

>grep PATTERN FILE

찾으려는 패턴을 파일에서 찾아줌

  • 예시
      ~/Mystudy/Linux ❯ grep "Chapter" Greatgatsby.txt
      1. Chapter 1
      1. Chapter 2
  • 예시
    ~/Mystudy/Linux ❯ grep "Gatsby" Greatgatsby.txt

Options

grep은 대소문자를 구분한다

i

구분 하지 않으려면 -i 옵션을 준다

w

제공한 패턴이 정확히 일치하는 단어 찾기 (포함파는X)
grep -w "cat" book.txt

r

재귀적 검색?
grep -r "chickin"

grep -ri "parm[ae]san" MealDiary/
MealDiary/ 아래에서 'parmasan' 또는 'parmesan' 을 찾음

c

count

~/Mystudy/Linux ❯ grep "myself" SongOfMyself.txt -ic
47
~/Mystudy/Linux ❯ grep "I" SongOfMyself.txt -c
439
~/Mystudy/Linux ❯ grep "I" SongOfMyself.txt -cw
392
~/Mystudy/Linux ❯ grep "I" SongOfMyself.txt -cw

A, B

-A num, --after-context=num
Print num lines of trailing context after each match. See also the
-B and -C options.

-B num, --before-context=num
Print num lines of leading context before each match. See also the
-A and -C options.

grep "wagon" SongOfMyself.txt -B4 : wagon 이전 4줄

grep의 Regex

.

. = 글자 1개

grep "p...." file.txt
p로 시작하고 다음 네글자 있는거 전부 출력

^ (carot)

행의 시작

  • 입력
    ~/Mystudy/Linux ❯ grep "^I" SongOfMyself.txt -w

  • 출력

    I celebrate myself, and sing myself,
    I loafe and invite my soul,
    I lean and loafe at my ease observing a spear of summer grass.
    I, now thirty-seven years old in perfect health begin,
    I harbor for good or bad, I permit to speak at every hazard,
    I breathe the fragrance myself and know it and like it,
    I will go to the bank by the wood and become undisguised and naked,
    I am mad for it to be in contact with me.
    I have heard what the talkers were talking, the talk of the beginning and the end,

$

행의 끝 문장의 끝

  • 입력
    ~/Mystudy/Linux ❯ grep ")$" SongOfMyself.txt -w
  • 출력
    You shall possess the good of the earth and sun, (there are millions of suns left,)
    (They do not know how immortal, but I know.)
    (He will never sleep any more as he did in the cot in his mother’s bed-room;)
    The young fellow drives the express-wagon, (I love him, though I do not know him;)
    The regatta is spread on the bay, the race is begun, (how the white sails sparkle!)

[abc]

parmesan 예시

확장정규식

-E 붙여야함

? : 있거나 없거나

~/Mystudy/Linux ❯ grep "bird" -w  SongOfMyself.txt
Where the mocking-bird sounds his delicious gurgles, cackles, screams, weeps,
Where the humming-bird shimmers, where the neck of the long-lived swan is curving and winding,

~/Mystudy/Linux ❯ grep "birds" -w  SongOfMyself.txt
I hear bravuras of birds, bustle of growing wheat, gossip of flames, clack of sticks cooking my meals,
And am stucco’d with quadrupeds and birds all over,
(They bore mites as for unfledg’d birds who have now to rise and fly and sing for themselves,)

~/Mystudy/Linux ❯ grep "birds?" -w  SongOfMyself.txt

### 확장정규식 사용
~/Mystudy/Linux ❯ grep "birds?" -wE  SongOfMyself.txt
I hear bravuras of birds, bustle of growing wheat, gossip of flames, clack of sticks cooking my meals,
And am stucco’d with quadrupeds and birds all over,
Where the mocking-bird sounds his delicious gurgles, cackles, screams, weeps,
Where the humming-bird shimmers, where the neck of the long-lived swan is curving and winding,
(They bore mites as for unfledg’d birds who have now to rise and fly and sing for themselves,)

{}

a{3} = aaa

[A-Z]{5} = 대문자로 쓰여진 5개 글자

Piping to Grep

  • 프로세스 중에서 쉽게 찾기
    ps -aux | grep "sound"
  • man 에서 원하는 부분 찾기
~/Mystudy/Linux ❯ man grep | grep "count"                         
             Only a count of selected lines is written to standard output.
             file, starting at line 1.  The line number counter is reset for
profile
냅다 써보는 공부의 흔적😇

0개의 댓글