如何在 Linux 中查詢不帶有檔名的字串?
我們知道,我們可以使用 grep 命令來搜尋檔案中或多個檔案中所有行中的特定字元模式。grep 命令以不區分大小寫的方式在檔案中搜索單詞。
讓我們看一個簡單的示例,在這個示例中,我們將在目錄中存在的所有檔案中搜索一個模式。
命令
grep -i ‘lp’ Sample*
輸出
Sample: The sample was printed via the internet. Sample: I believe lp cares about what the device is. Sample1: This was printed via the internet. Sample1: I believe lp cares about what the device is. Sample2: This was printed via the internet. Sample3: I believe lp cares about what the device is.
對於這種情況,唯一的問題是我們不希望檔名隨輸出一起列印。我們只需要模式被找到的字串,我們需要列印該字串即可。
在這種情況下,我們可以使用-h引數,該引數可用於隱藏檔名。
命令
grep -i -hn ‘lp’ Sample*
根據 Linux 文件,-h 標記的作用如下,
“-h,--no-filename
禁止在輸出中新增檔名作為字首。如果要搜尋的檔案只有一個(或者只有標準輸入),則這是預設設定。”
輸出
The sample was printed via the internet. I Believe lp cares about what the device is. This was printed via the internet. I believe lp cares about what the device is. This was printed via the internet. I believe lp doesn't care what the device is
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP