راهنمای جستجو درون فایلهای متنی در لینوکس
برای جستجوی فایلها در لینوکس ابزارهای مختلفی وجود دارند که این کار را به خوبی انجام میدهند، اما اگر قرار باشد محتویات درون فایلها را جستجو کنیم به چه ابزارهایی نیاز داریم؟
باز هم لینوکس به کمک ابزارهای قدرتمندش به ما کمک خواهد کرد تا به سرعت به جواب دلخواهمان برسیم. برای این کار راهها و ابزارهای متفاوتی در دسترسند اما در این پست فقط به معرفی ابزار ساده اما قدرتمند grep اکتفا میکنم.
جستجوهای زیر در فایلی شامل مجوز GPL نسخهی ۲ انجام میشود.
syntax بسیار سادهی زیر این کار را انجام میدهد.
grep word file-name
برای مثال اگر بخواهیم در فایل نمونه به دنبال کلمهی GNU بگردیم از دستور زیر استفاده میکنیم. به خاطر داشته باشید که این دستور به حروف کوچک و بزرک حساس است.
$ grep GNU gpl-v2.txt
GNU GENERAL PUBLIC LICENSE
The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too.
modify it under the terms of the GNU General Public License
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License.
همانطور که میبینید در خروجی تمام خطوطی که کلمهی GNU در آنها بود با هایلایت کردن این کلمه ظاهر شدهاند.
برای اینکه grep به حروف بزرگ و کوچک حساس نباشد باید از سوئیچ i استفاده کنید.
$ grep -i gnu gpl-v2.txt
GNU GENERAL PUBLIC LICENSE
The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too.
modify it under the terms of the GNU General Public License
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License.
برای اینکه به دنبال بیشتر از یک کلمه بگردید از روش زیر استفاده کنید.
$ grep 'received a copy' gpl-v2.txt
You should have received a copy of the GNU General Public License
میتوانید جستجو را در بیشتر از یک فایل انجام دهید.
$ grep word file-name-1 file-name-2
یا در تمام فایلها جستجو کنید.
$ grep word *.txt
با استفاده از grep حتی میتوانید جستجو را در تمام خطوطی انجام دهیم که کلمهی مورد نظر را نداشته باشند.
$ grep -v word file-name
ممکن است بخواهید ببینید یک کلمه چند بار در متن ظاهر شده است.
$ grep -c "GNU" gpl-v2.txt
6
grep حتی میتواند به شما شمارهی خطوطی که کلمهی مورد نظر در آنها ظاهر شده است را نشان دهد.
$grep -n "GNU" gpl-v2.txt
1:GNU GENERAL PUBLIC LICENSE
13:The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too.
98:modify it under the terms of the GNU General Public License
105:GNU General Public License for more details.
107:You should have received a copy of the GNU General Public License
133:This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License.