Matn qidirish (grep)


ULASHISH

grep buyrug‘idan foydalanish

grep buyrug‘i fayllar ichidan matn shablonlarini qidirish uchun ishlatiladi.

Bu katta fayllardan yoki ko‘plab fayllar orasidan kerakli matnni topishning kuchli usuli.


Asosiy foydalanish

Fayldan shablonni qidirish uchun grep 'pattern' filename buyrug‘idan foydalaning:

Misol

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)

Parametrlar

grep buyrug‘ida uning ishlashini o‘zgartiruvchi parametrlar mavjud:

  • -i - Harf registrini (katta yoki kichik harf) hisobga olmasdan qidirish
  • -r - Katalog va uning ichki kataloglaridagi barcha fayllar bo‘ylab qidirish
  • -v - Shablonga mos kelmaydigan qatorlarni topish

Registrni hisobga olmaslik

-i parametri harf registriga e’tibor bermasdan qidirish imkonini beradi.

Misol: registrni hisobga olmaslik

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)


Rekursiv qidiruv

-r parametri katalog va uning ichki kataloglaridagi barcha fayllar bo‘ylab qidirish imkonini beradi.

Misol: rekursiv qidiruv

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)

Teskari moslik

-v parametri shablonga mos kelmaydigan qatorlarni topadi.

Misol: teskari moslik

grep -v 'shell' my_file.txt
Understanding Shells

grep buyrug‘ini regulyar ifodalar bilan ishlatish

Regulyar ifodalar murakkab shablonlarni qidirish imkonini beradi.

Masalan, grep '^[A-Za-z]' file.txt buyrug‘i harf bilan boshlanadigan qatorlarni topadi.

Misol: regulyar ifodalar

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.


W3Schools Pathfinder

Yutuqlaringizni kuzating – bu bepul!