簡體   English   中英

如何在文件中搜索模式並在終端上顯示行?

[英]how to search for pattern in file and display the line on terminal?

我有包含如下內容的filw:

pid 4565 Process: /my/name/is/4678 +34787[d]sfh888dwe4rtertfsj@##$
pid 33453 Process: /my/name/is/4678 +34787[d]sfh888dfsj@##werwer$
pid 3453345 Process: /my/name/is/4678 +3478[7]dsfhew46534wy6888dfsj@##$
pid 12335 Process: /my/name/is/4678 +3478se[r]tet57dsfh888dfsj@##$

我需要在行中包含“ +34787 [d] sfh888dfsj @ ## werwer $”這種模式,並需要在終端上獲得整個行,或者可以將其重定向到另一個文件。 有什么建議嗎? 提前致謝。

將grep與固定字符串模式(也稱為fgrep)一起使用:

grep -F '+34787[d]sfh888dfsj@##werwer$' /path/to/my/file

運行man grep以了解更多信息。

如果需要將其重定向到某個輸出文件而不是標准輸出:

grep -F '+34787[d]sfh888dfsj@##werwer$' /path/to/my/file > /path/to/my/output

運行man shman bash並閱讀有關重定向的信息。

grep / awk / sed ...許多工具都可以做到這一點。 grep示例:

 kent$  echo "pid 4565 Process: /my/name/is/4678 +34787[d]sfh888dwe4rtertfsj@##$
    pid 33453 Process: /my/name/is/4678 +34787[d]sfh888dfsj@##werwer$
    pid 3453345 Process: /my/name/is/4678 +3478[7]dsfhew46534wy6888dfsj@##$
    pid 12335 Process: /my/name/is/4678 +3478se[r]tet57dsfh888dfsj@##$"|grep '\+34787\[d\]sfh888dfsj@##werwer\$'

    pid 33453 Process: /my/name/is/4678 +34787[d]sfh888dfsj@##werwer$

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM