繁体   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