Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux grep 指令實戰範例匯總:從套件搜尋到設定檔過濾註解

編輯:關於 Linux

grep 是 Linux 最強大的文字模式搜尋工具。本文透過兩大實戰場景展示用法:場景一 結合 dpkg -l | grep -i python 快速查詢系統安裝的 Python 版本與模組;場景二 用 grep -v '^#' /etc/apache2/sites-available/default-ssl 移除設定檔註解行,產出乾淨內容供除錯參考。並說明 -i 忽略大小寫、管道串聯、-v 反向匹配等核心技巧。

實戰指令速查

需求指令範例關鍵參數
查詢已安裝 Python 套件dpkg -l | grep -i python-i 忽略大小寫
移除設定檔註解行grep -v '^#' file.conf-v 反向匹配
遞迴搜尋目錄下關鍵字grep -r "keyword" /path-r 遞迴
顯示行號與匹配內容grep -n "pattern" file-n 行號

善用 grep 與管道組合,能將龐大輸出快速過濾為所需資訊,是系統管理與開發除錯的必備技能。

Copyright © Windows教程網 All Rights Reserved