Linux 我想使用“awk”或 sed 打印文件中所有以“comm=”开头的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8326817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:38:41  来源:igfitidea点击:

I want to use "awk" or sed to print all the lines that start with "comm=" in a file

linuxshellscriptingsedawk

提问by wael

I want to use "awk" or "sed" to print all the lines that start with comm=from the file filex, Note that each line contains "comm=somthing"

我想使用“awk”或“sed”来打印comm=从文件开始的所有行filex,注意每一行都包含“comm=somthing”

for example : comm=rm , comm=ll, comm=ls  ....

How can i achieve that ?

我怎样才能做到这一点?

回答by jaypal singh

For lines that start with comm=

对于以开头的行 comm=

sed -n '/^comm=/p' filex

awk '/^comm=/' filex

If comm=is anywhere in the line then

如果comm=在该行中的任何位置,则

sed -n '/comm=/p' filex

awk '/comm=/' filex

回答by Cédric Julien

You could use grepalso :

您也可以使用grep

grep comm= filex

this will display all the lines containing comm=.

这将显示所有包含comm=.

回答by Michael J. Barber

Here's an approach using grep:

这是使用 grep 的一种方法:

grep -o '\<comm=[[:alnum:]]*\>'

This treats a word as consisting of alphanumeric characters; extend the character class as needed.

这将一个单词视为由字母数字字符组成;根据需要扩展字符类。

回答by nbari

If grepis ok to use, you could give a try to:

如果grep可以使用,您可以尝试:

grep -E "^comm=" file