grep - Search Text Using Patterns¶
Menggunakan Perintah grep¶
Perintah grep digunakan untuk mencari pola teks di dalam berkas.
Ini adalah cara ampuh untuk menemukan teks tertentu dalam berkas besar atau di antara banyak berkas.
Penggunaan Dasar¶
Untuk mencari pola dalam berkas, gunakan grep 'pattern' namaberkas:
$ grep 'shell' file.txt
A shell is a text-based interface that lets you talk to your computer.
There are different types of shells. Bash (Bourne Again SHell)
Opsi¶
Perintah grep memiliki opsi untuk mengubah cara kerjanya:
-i - Pencarian dengan mengabaikan perbedaan huruf besar/kecil (huruf kapital atau huruf kecil)
-r - Pencarian melalui semua berkas dalam direktori dan subdirektorinya
-v - Menemukan baris yang tidak sesuai dengan pola
Ignore Case¶
Opsi -i memungkinkan Anda mencari tanpa perlu mengkhawatirkan perbedaan huruf besar/kecil.
$ grep -i 'shell' file.txt
Understanding Shells
A shell is a text-based interface that lets you talk to your computer.
There are different types of shells. Bash (Bourne Again SHell)
Recursive Search¶
Opsi -r memungkinkan Anda mencari melalui semua berkas dalam suatu direktori dan subdirektorinya.
$ grep -r 'search_term' /home/user/my_directory
/home/user/.my_secret_file:A shell is a text-based interface that lets you talk to your computer.
/home/user/.my_secret_file:There are different types of shells. Bash (Bourne Again SHell)
/home/user/.my_secret_file:Bourne Shell (sh): The original Unix shell, developed by Stephen Bourne.
/home/user/copy_of_my_file.txt:A shell is a text-based interface that lets you talk to your computer.
/home/user/copy_of_my_file.txt:There are different types of shells. Bash (Bourne Again SHell)
/home/user/myfolder/my_file.txt:A shell is a text-based interface that lets you talk to your computer.
/home/user/myfolder/my_file.txt:There are different types of shells. Bash (Bourne Again SHell)
/home/user/my_file.txt:A shell is a text-based interface that lets you talk to your computer.
/home/user/my_file.txt:There are different types of shells. Bash (Bourne Again SHell)
Invert Match¶
Opsi -v menemukan baris yang tidak cocok dengan pola.
$ grep -v 'shell' my_file.txt
Understanding Shells
Using grep with Regular Expressions¶
Ekspresi reguler memungkinkan Anda mencari pola yang kompleks.
Misalnya, grep '^[A-Za-z]' file.txt menemukan baris yang dimulai dengan huruf.
$ grep '^[A-Za-z]' my_file.txt
Understanding Shells
A shell is a text-based interface that lets you talk to your computer.
There are different types of shells. Bash (Bourne Again SHell)
is popular because it's powerful and easy to use.