ค้นหาคำที่ต้องการในไฟล์


เคยสงสัยไหมว่าถ้าเรามี text file เยอะๆในเครื่องแล้วเราเกิดอยากหาคำหนึ่งคำว่ามันอยู่ใน text file ไหนบ้างเราจะทำยังไง เช่น สมมติผมอยากหาคำว่า "iframe" ในไฟล์เว็บของผม โดยอยากรู้ว่าในไฟล์ในไหนบ้างที่มีคำว่า "iframe" อยู่ ไม่ต้องไม่หาโปรแกรมตัวไหนมาติดตั้งเพิ่มครับ Command line ช่วยคุณได้

โดยผมจะใช้คำสั่ง grep ช่วยเหลือโดย systax จะเป็นดังนี้

grep "text string to search" directory-path

ตัวอย่างครับ

หาในไฟล์ที่ต้องการ

grep "iframe" /var/www/html/*.php

ค้นหา subdirectory ย่อยทั้งหมดให้เพิ่ม "-r"

grep -r "iframe" /var/www/html/*.php

สิ่งที่ได้ออกมา

...
filename.php: iframe
...

และถ้าเราต้องการอยากรู้แค่ชื่อไฟล์ให้ใช้คำสั่งนี้ครับ

grep -H "iframe" /var/www/html/ -R | cut -d: -f1

สิ่งที่ได้ออกมา

...
filename.php
...

ลองนำไปประยุกต์ใช้กันน่ะครับ

Thank : http://www.cyberciti.biz/faq/howto-search-find-file-for-text-string/


Comments

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • HTML tags will be transformed to conform to HTML standards.

More information about formatting options

CAPTCHA
คำถามนี้ทดสอบว่าคุณมีความรู้ทางคณิตศาสตร์ ไม่ได้โง่!!!
5 + 4 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.