Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Linux grep/ack/ag 搜尋字串用法與範例 | ShengYu Talk

首頁
Python教學
C/C++教學
歸檔
標籤
Projects
關於我

首頁 Python教學 C/C++教學 歸檔 標籤 Projects 關於我

    

ShengYu Talk

2019-12-02

Linux grep/ack/ag 搜尋字串用法與範例

本篇 ShengYu 將介紹如何使用 Linux 下的 grep/ack/ag 指令來搜尋字串,並且將搜尋結果的檔案


同時進行移動/複製/刪除。

搜尋目錄下特定樣式的字串

1 # 使用 grep
2 grep -rI "foo" *
3 # 使用 ack (Ack-Grep)
4 ack "foo"
5 # 使用 ag (The Silver Srarcher)
6 ag "foo"

搜尋目錄下檔案內有特定樣式的字串,並且將這些檔案移
動mv/複製cp/刪除rm

1 # 搜尋包含hello關鍵字的檔案,並且將這些檔案移動到tmp資料夾

https://shengyu7697.github.io/linux-grep-ack-ag/[2023/2/24 上午 01:14:19]
Linux grep/ack/ag 搜尋字串用法與範例 | ShengYu Talk

2 $ ack "hello" ./ -l --print0 | xargs -0 -i mv {} tmp


3
4 # 刪除包含 hello 關鍵字的檔案
5 $ ack "hello" ./ -l --print0 | xargs -0 rm

搜尋目錄下檔案內有特定樣式的字串,並且將這些檔案進行取代

1 # 把目錄下所有檔案文字中包含 "oldtext" 的 取代成 "newtext"


2 $ ag oldtext -l0 | xargs -0 sed -i 's/oldtext/newtext/g'
3 $ grep -rl oldtext . | xargs sed -i 's/oldtext/newtext/g'

其它相關文章推薦
Linux 常用指令教學懶人包
Linux cut 字串處理用法與範例
Linux sed 字串取代用法與範例
Linux find 尋找檔案/尋找資料夾用法與範例

#Linux #Linux Command #Shell Script #Ubuntu  Share

NEWER
在 Ubuntu 下編譯安裝 OpenCV 3.4.8

OLDER
Linux find 尋找檔案/尋找資料夾用法與範例

精選文章

Python 基礎教學目錄
C/C++ 入門教學目錄
Linux 常用指令教學懶人包

https://shengyu7697.github.io/linux-grep-ack-ag/[2023/2/24 上午 01:14:19]
Linux grep/ack/ag 搜尋字串用法與範例 | ShengYu Talk

最新文章

Ubuntu Android Emulator 模擬器的 image 大小


Java File 用法與範例
JavaScript onclick event submit form 用法範例
Android Java 取得 build date 當下編譯日期時間
6 種查詢 SQLite 版本的方法

粉絲專頁

ShengYu
追蹤粉絲專頁 141 位追蹤者

分類

Arduino (4)
C/C++教學 (166)
Github (14)
Git教學 (20)
OpenCV教學 (8)
Projects (6)
Python教學 (226)
Qt教學 (7)

https://shengyu7697.github.io/linux-grep-ack-ag/[2023/2/24 上午 01:14:19]
Linux grep/ack/ag 搜尋字串用法與範例 | ShengYu Talk

© 2023 ShengYu
Powered by Hexo

https://shengyu7697.github.io/linux-grep-ack-ag/[2023/2/24 上午 01:14:19]

You might also like