0Pricing
Linux Command Line Mastery · Pelajaran

Memfilter Teks dengan `grep`

Kuasai `grep` untuk mencocokkan pola dan memfilter teks dalam berkas atau keluaran perintah.

Memfilter Teks dengan `grep` adalah pelajaran Linux Command Line Mastery gratis di CoddyKit. Ini adalah pelajaran 2 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Linux Command Line Mastery, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Linux Command Line Mastery mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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!

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Memfilter Teks dengan `grep`” gratis?

Ya — teks lengkap “Memfilter Teks dengan `grep`” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Linux Command Line Mastery, upgrade ke CoddyKit PRO. Kursus Linux Command Line Mastery mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Memfilter Teks dengan `grep`”?

Kuasai `grep` untuk mencocokkan pola dan memfilter teks dalam berkas atau keluaran perintah. Kamu berlatih Linux Command Line Mastery dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Linux Command Line Mastery?

Tidak diperlukan pengalaman sebelumnya. Linux Command Line Mastery di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 2 dari 4.

Berapa lama pelajaran “Memfilter Teks dengan `grep`” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Linux Command Line Mastery ini?

Ya. Setiap pelajaran Linux Command Line Mastery menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Aliran Standar dan Pengalihan
  2. Memfilter Teks dengan `grep`
  3. Manipulasi Teks dengan `sed` dan `awk`
  4. Menggabungkan dan Mengurutkan Keluaran dengan sort, uniq, dan cut
← Kembali ke Linux Command Line Mastery