`grep`によるテキストの絞り込み
ファイル内のテキストやコマンド出力に対して、強力なパターンマッチングと絞り込みを行う`grep`を使いこなします。
「`grep`によるテキストの絞り込み」はCoddyKit上の無料Linux Command Line Masteryレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.txtSearching 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" *.logCase-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.txtFinding 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.pyCounting 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.logLocating 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時間対応のAIチューター)、Linux Command Line Masteryコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Linux Command Line Masteryコースには全4レッスンが含まれています。
「`grep`によるテキストの絞り込み」で何を学びますか?
ファイル内のテキストやコマンド出力に対して、強力なパターンマッチングと絞り込みを行う`grep`を使いこなします。 ブラウザで直接実行するハンズオンコードでLinux Command Line Masteryを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Linux Command Line Masteryを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのLinux Command Line Masteryは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「`grep`によるテキストの絞り込み」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このLinux Command Line Masteryレッスンでコードを書いて実行できますか?
はい。すべてのLinux Command Line Masteryレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 標準ストリームとリダイレクト
- `grep`によるテキストの絞り込み
- `sed`と`awk`によるテキスト操作
- sort、uniq、cut で出力を結合・並べ替える