صفر و یک - برنامه نویسی - لینوکس

دست نوشته های شخصی من در مورد نرم افزار، سیستم عامل و برنامه نویسی لینوکس

صفر و یک - برنامه نویسی - لینوکس

دست نوشته های شخصی من در مورد نرم افزار، سیستم عامل و برنامه نویسی لینوکس

لیدورا یک وبلاگ تخصصی برنامه نویسی یا لینوکس نیست. بیشتر محلیه برای ثبت چیزهایی که قراره بیشتر از یه بار خونده بشن. اگه به درد شما هم خورد که زکات دانش ما پرداخت شده، شکر.

آخرین نظرات

نویسندگان

grep command

برای جستجوی فایل‌ها در لینوکس ابزارهای مختلفی وجود دارند که این کار را به خوبی انجام می‌دهند، اما اگر قرار باشد محتویات درون فایل‌ها را جستجو کنیم به چه ابزارهایی نیاز داریم؟

باز هم لینوکس به کمک ابزارهای قدرتمندش به ما کمک خواهد کرد تا به سرعت به جواب دلخواهمان برسیم. برای این کار راه‌ها و ابزارهای متفاوتی در دسترسند اما در این پست فقط به معرفی ابزار ساده اما قدرتمند 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. 

 

منابع : + +

نظرات  (۱۱)

چرا این مطلب توی فید من نیومده ؟! میخواستم بیام دعوا راه بندازم چرا چند روزه مطلب نذاشتی ! :) شانست گفت.
پاسخ:
:)
فکر نکنم مشکلی باشه. اون مطلب بعدی هم نیومده؟
این یعنی چی؟! 

با استفاده از grep حتی می‌توانید جستجو را در تمام خطوطی انجام دهیم که کلمه‌ی مورد نظر را نداشته باشند.


پاسخ:
یعنی خروجیش تمام خطوطی از فایله که اون کلمه توش نیست. جستجوی برعکس.
بعضی وقتا نیاز هست که با الگو جستجو کرد، مثل شما یه صفحه‌ی وب رو ذخیره کردی که در اون آدرس‌هایی به صورت زیر برای دانلود هست:
http://filan.ir/ad/mov1.mp4
اینجا میشه از سوئیچ‌های e و o دستور grep استفاده کرد و آدرس‌ها رو جدا کرد:
grep -e 'http://filan.ir/ad/mov[0-9]*.mp4' html_file
این میاد همونطور که گفتین همه‌ی سطرهایی که آدرسی مشابه با http://filan.ir/ad/mov1.mp4 و http://filan.ir/ad/mov32.mp4 و http://filan.ir/ad/mov418.mp4 و … رو داره رو پیدا میکنه و نشون میده. [0-9]‎ که به جای عدد گذاشتیم یعنی از ۰ تا ۹ با هر تکرار (* یعنی هر تکرار) رو برام پیدا کن.
خب حالا با سوئیچ o هم میگیم که فقط اون عبارت رو نشون بده و نه تمام سطر:
grep -o -e 'regular_expression' file
حالا ممکنه که داده‌های تکراری داشته باشیم، با دستور uniq میشه این مشکل رو حل کرد:
grep -o -e 'regular_expression' file | uniq

ممنونم به خاطر توضیح ساده و شیواتون از این دستور.
پاسخ:
ممنون خودم جان عزیز
اشاره‌ی بسیار کاربردی کردید. grep یکی از دستورات مورد علاقه‌ی خودم هم هست به همین خاطر که تقریباَ هر کاری رو می‌شه باهاش کرد.
به امید خدا از دستور شما در همین پست یا در پست‌های آینده استفاده خواهم کرد.

زنده باشید.
درست شد.
پاسخ:
خدا رو شکر :)
راستی یه سوال. آقا این nautilus بد رو اصابه!‌ قبلنا با backspace برمیگشت عقب. یا مثلا یه حرف تایپ میکردی سریع یه پوشه یا یه فایلی رو انتخاب میکرد ولی الان خیلی اصاب خورد کن شده. نظرت ؟‌:)
پاسخ:
سعید جان با کلیدهای Alt+Left و Alt+Right میتونی عقب و جلو بری همین طور کلیدهای Alt+UP و Alt+DOWN اما اگه همون BackSpace رو میخوای این راهش:

این دستور رو بزن:

gedit ~/.config/nautilus/accels


حالا این خط رو پیدا کن:

;(gtk_accel_path "<Actions>/ShellActions/Up" "<Alt>Up")


BackSpace رو بذار جای Alt>Up> و اون ; اول خط رو بردار تا این شکلی بشه:

(gtk_accel_path "<Actions>/ShellActions/Up" "BackSpace")


فایل رو ذخیره کن تا تغییرات اعمال بشه
علی نمو نصب کردم ! سرچ nautilus خیلی رو اصاب بود. ممنون.
پاسخ:
در خدمتیم
سلام این سرچ کردن برای متن فارسی هم جواب میده؟ یا یه روش دیگه داره
سلام
یه کمکی میخواستم اگه کسی بلده کمکم کند
ابزار سرج در webدر خط ترمینال چی میشه؟
hosseinh1371@gmail.com
پاسخ:
از این اسکریپت استفاده کنید.

#!/bin/bash

if [[ $(echo $*) ]]; then

    searchterm="$*"

else

    read -p "Enter your search term: " searchterm

fi

searchterm=$(echo $searchterm | sed -e 's/\ /+/g')

lynx -dump http://www.google.com/search?q=$searchterm | less

سلام
ممنون :)
پاسخ:
سلام امین جان. زنده باشی عزیز.
بچه ها این سوالی که دوستمون پرسید ابزار سرچ تو Web 
حالا هردفعه که بخوایم سرچ کنیم باید این کدوبزنیم؟؟؟؟؟
۲۶ آذر ۹۶ ، ۱۸:۴۵ فایل مارکت
مرسی عالی بود
پاسخ:
خداروشکر که به دردتون خورد.

ارسال نظر

ارسال نظر آزاد است، اما اگر قبلا در بیان ثبت نام کرده اید می توانید ابتدا وارد شوید.
شما میتوانید از این تگهای html استفاده کنید:
<b> یا <strong>، <em> یا <i>، <u>، <strike> یا <s>، <sup>، <sub>، <blockquote>، <code>، <pre>، <hr>، <br>، <p>، <a href="" title="">، <span style="">، <div align="">
تجدید کد امنیتی