05-查詢、管道和重定向
阿新 • • 發佈:2021-01-09
技術標籤:Linux
查詢、管道和重定向
- 查詢:grep
- 管道:|
- 重定向:>> 和 >
01.查詢grep
- grep 與 find命令不同,grep是查詢檔案內容,命令格式如下
# 查詢字串是否在檔案中,如果在則打印出來
grep 字串 檔名
例如:查詢 test.py 中出現 fu的行
grep fu test.py -n
02.管道
-
管道的作用是將查詢到的資料作為輸入,我是這樣理解的 ==> 輸入 | 輸出
-
管道經常配合grep來使用,篩選出需要的內容
例如:檢視當前目錄下是所有的.py檔案中是否存在 1.py
[email protected]:~/Desktop$ ls *.py | grep 1.py
03.重定向
-
將查詢的內容輸出到文字中,不在終端顯示
-
>> 表示不刪除文字原來內容,將查詢結果追加到後面
-
> 刪除文字原來內容,然後將查詢結果列印到文字中
例子:查詢 test.py 中所有出現 hello 的行,並將其列印到find.txt中
[email protected]:~/Desktop$ cat find.txt
cat: find.txt: 沒有那個檔案或目錄
[email protected]:~/Desktop$ cat test.py
def aa_fu():
print("hello world")
print("hello world")
print("hello world")
print("hello world")
print("hello world")
[email protected]:~/Desktop$ grep hello test.py >> find.txt -n
[email protected]:~/Desktop$ cat find.txt
2: print("hello world")
3: print("hello world")
4: print("hello world")
5: print("hello world")
6: print("hello world")
[email protected] :~/Desktop$