0Pricing
Linux Command Line Mastery · 강의

`grep`으로 텍스트 필터링

파일이나 명령 출력 내 텍스트에서 패턴을 강력하게 일치시키고 필터링하는 `grep` 사용법을 익힙니다.

`grep`으로 텍스트 필터링은(는) CoddyKit의 무료 Linux Command Line Mastery 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Linux Command Line Mastery 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Linux Command Line Mastery 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

What is `grep`?

Welcome to the world of text filtering! In this lesson, we'll master the mighty grep command.

grep stands for Global Regular Expression Print. It's a powerful command-line utility for searching plain-text data sets for lines that match a regular expression.

  • It helps you quickly find specific information in files.
  • It filters output from other commands.
  • It's indispensable for system administration, programming, and data analysis.

Your First `grep` Search

The most basic use of grep is to search for a specific word or pattern within a file.

The general syntax is: grep "pattern" filename

Let's find all lines containing the word "error" in a log file. Imagine my_log.txt contains various system messages.

echo "System started.
Error: Disk full.
Warning: Low memory.
Error: Network down." > my_log.txt
grep "Error" my_log.txt

Searching Multiple Files

grep isn't limited to a single file. You can search across multiple files or even all files in a directory.

To search multiple files, simply list them after the pattern. To search all files in the current directory, use a wildcard like *.

Let's search for "failed" in all .log files.

echo "Login success" > auth.log
echo "Login failed" > secure.log
echo "System OK" > kernel.log
grep "failed" *.log

Case-Insensitive Search (`-i`)

By default, grep is case-sensitive. This means "Error" is different from "error" or "ERROR".

To perform a case-insensitive search, use the -i option (for ignore-case).

Try finding all instances of "apple", regardless of how it's capitalized.

echo "Apple is good.
I like apples.
APPLES are red." > fruit.txt
grep -i "apple" fruit.txt

Finding What DOESN'T Match (`-v`)

Sometimes you want to see all lines that *do not* contain a specific pattern. This is called an inverted match.

The -v option (for invert-match) does exactly this. It shows all lines that *don't* match the given pattern.

Let's filter out lines that contain "comment" from a script.

echo "# This is a comment
print("Hello")
# Another comment
x = 10" > script.py
grep -v "#" script.py

Counting Occurrences (`-c`)

Instead of showing the matching lines, you might just want to know how many lines match your pattern.

The -c option (for count) tells grep to print only a count of the lines that match the pattern.

How many lines contain "warning" in our system log?

echo "Info: System started.
Warning: Low disk space.
Info: User logged in.
Warning: High CPU usage." > system.log
grep -c "Warning" system.log

Locating Matches with Line Numbers (`-n`)

When you're debugging or analyzing a large file, knowing the exact line number where a match occurs can be very helpful.

The -n option (for line-number) displays the line number before each matching line.

Let's find the line numbers for "function" in a program file.

echo "def setup():
  print("Setting up")
def run_function():
  pass
# Main part" > program.py
grep -n "function" program.py

`grep` with Standard Input

One of grep's most powerful features is its ability to work with pipes (|).

You can pipe the output of one command as input to grep, allowing you to filter command output in real-time.

Let's list all files and then filter to show only those ending with .txt.

touch report.txt document.pdf image.jpg notes.txt
ls -l | grep ".txt"

`grep` Quick Check

You've learned several useful grep options. Let's test your understanding!

Consider a file named server.log which contains messages like "ERROR", "Error", "info", etc.

Recap: Filtering with `grep`

Great job! You've mastered the fundamentals of grep for filtering text.

Here's a quick summary of what we covered:

  • grep "pattern" filename: Basic search for a pattern in a file.
  • grep "pattern" *.log: Search across multiple files using wildcards.
  • -i: Ignore case during the search.
  • -v: Invert the match, showing lines that *don't* contain the pattern.
  • -c: Count the number of matching lines.
  • -n: Display line numbers for matching lines.
  • Piping with |: Filter output from other commands.

These options make grep an incredibly versatile tool for extracting and analyzing information on the command line. Keep practicing!

자주 묻는 질문

“`grep`으로 텍스트 필터링” 강의는 무료인가요?

네 — “`grep`으로 텍스트 필터링” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Linux Command Line Mastery 강의 전체를 잠금 해제할 수 있습니다. Linux Command Line Mastery 강의에는 총 4개의 강의가 포함되어 있습니다.

“`grep`으로 텍스트 필터링”에서 뭘 배우나요?

파일이나 명령 출력 내 텍스트에서 패턴을 강력하게 일치시키고 필터링하는 `grep` 사용법을 익힙니다. 브라우저에서 직접 실행하는 실습 코드로 Linux Command Line Mastery을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Linux Command Line Mastery을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Linux Command Line Mastery은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“`grep`으로 텍스트 필터링” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Linux Command Line Mastery 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Linux Command Line Mastery 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 표준 스트림 및 리디렉션
  2. `grep`으로 텍스트 필터링
  3. `sed`와 `awk`로 텍스트 조작
  4. sort, uniq, cut으로 출력 결합하고 정렬하기
← Linux Command Line Mastery(으)로 돌아가기