Skip to content

필사 모드: Linux grep Command Basics

English
0%
정확도 0%
💡 왼쪽 원문을 읽으면서 오른쪽에 따라 써보세요. Tab 키로 힌트를 받을 수 있습니다.
원문 렌더가 준비되기 전까지 텍스트 가이드로 표시합니다.

Overview

Linux has a useful tool called `grep`. By making good use of this tool, you can extract useful information from text files.

grep document

$ grep -h

usage: grep [-abcdDEFGHhIiJLlMmnOopqRSsUVvwXxZz] [-A num] [-B num] [-C[num]]

[-e pattern] [-f file] [--binary-files=value] [--color=when]

[--context[=num]] [--directories=action] [--label] [--line-buffered]

[--null] [pattern] [file ...]

options

- `-A num` --after-context: Display num additional lines after the matched line

- `-B num` --before-context: Display num additional lines before the matched line

- `-C num` --context: Display num lines before and after the matched line

- `-c` --count: Count the number of matched lines

- `-E pattern` --regexp=pattern: Output lines matching the regular expression pattern

- `-f file` --file=file: Read pattern from file

- `-r` --recursive: Recursively search subdirectories

- `-n` --line-number: Display line numbers

- `-v pattern`: Output all lines except those matching the pattern

examples

Let's assume we have a `test.txt` file with the following content.

$ cat test.txt

apple is sweety

banana is long

apple mango

pitch is

water melon is expensive

grapes are cheep

pear

grep pattern -A num text

$ grep apple -A 2 test.txt

apple is sweety

banana is long

apple mango

pitch is

water melon is expensive

grep pattern -B num text

$ grep apple -B 1 test.txt

apple is sweety

banana is long

apple mango

grep pattern -C num text

$ grep apple -C 1 test.txt

apple is sweety

banana is long

apple mango

pitch is

grep pattern -c text

grep apple -c test.txt

2

grep -e pattern text

$ grep -e "ap" test.txt

apple is sweety

apple mango

grapes are cheep

grep -f file text

$ cat regex.txt

apple

$ grep -f regex.txt test.txt

apple is sweety

apple mango

grep -n pattern text

$ grep -n appl test.txt

1:apple is sweety

3:apple mango

grep -v pattern word

Output lines excluding those containing "appl".

$ grep -v appl test.txt

banana is long

pitch is

water melon is expensive

grapes are cheep

pear

Combining with other commands

with greater-than (redirection)

Save grep results to out

$ grep -v appl test.txt > out.txt

$ cat out.txt

banana is long

pitch is

water melon is expensive

grapes are cheep

pear

with awk

Print only the first word

$ grep -v appl test.txt | awk '{print $1}'

banana

pitch

water

grapes

pear

with sort, uniq

Sort the first word and remove duplicates

$ grep ap test.txt | awk '{print $1}' | sort | uniq

apple

grapes

If you want to study or test regex further, check out [regexr.com](https://regexr.com/).

Quiz

Learn the basics of grep, a useful text search command-line tool in Linux.

-A num --after-context: Display num additional lines after the matched line -B num

--before-context: Display num additional lines before the matched line -C num --context: Display

num lines before and after the matched line -c --count: Count the number of matched lines -E

pattern...

Output lines excluding those containing "appl". Combining with other commands

Save grep results to out

Print only the first word

현재 단락 (1/88)

Linux has a useful tool called `grep`. By making good use of this tool, you can extract useful infor...

작성 글자: 0원문 글자: 2,638작성 단락: 0/88