过滤 Linux 命令输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4712521/
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-05 02:30:14  来源:igfitidea点击:

Filtering Linux command output

linux

提问by Raajkumar

I need to get a row based on column value just like querying a database. I have a command output like this,

我需要根据列值获取一行,就像查询数据库一样。我有这样的命令输出,

Name ID Mem VCPUs State
Time(s)

Domain-0 0 15485 16 r----- 1779042.1

prime95-01 512 1
-b---- 61.9

名称 ID Mem VCPUs State
Time(s)

域-0 0 15485 16 r----- 1779042.1

prime95-01 512 1
-b---- 61.9

Here I need to list only those rows where state is "r". Something like this,

在这里,我只需要列出 state 为“r”的那些行。像这样的东西,

Domain-0 0 15485 16
r----- 1779042.1

域-0 0 15485 16
r----- 1779042.1

I have tried using "grep" and "awk" but still I am not able to succeed.

我曾尝试使用“grep”和“awk”,但仍然无法成功。

Please help me on this issue.

请帮助我解决这个问题。

Regards, Raaj

问候, 拉吉

回答by trojanfoe

try:

尝试:

awk ' ~ /^r.*/ { print }' 

Like this:

像这样:

cat file | awk ' ~ /^r.*/ { print }' 

回答by dtech

There is a variaty of tools available for filtering.

有多种工具可用于过滤。

If you only want lines with "r-----" grep is more than enough:

如果您只想要带有“r-----”的行,grep 就足够了:

command | grep "r-----"

Or

或者

cat filename | grep "r-----"

Quotes are optional but might prevent grep tripping over the -'s

引号是可选的,但可能会防止 grep 被 - 绊倒

回答by Patrick Echterbruch

grep solution:

grep解决方案:

command | grep -E "^([^ ]+ ){4}r"

What this does (-E switches on extended regexp):

这是做什么的(-E 打开扩展正则表达式):

The first caret (^) matches the beginning of the line. [^ ] matches exactly one occurence of a non-space character, the following modifier (+) allows it to also match more occurences.

第一个插入符号 (^) 匹配行的开头。[^ ] 匹配非空格字符的一次出现,以下修饰符 (+) 允许它也匹配更多次出现。

Grouped together with the trailing space in ([^ ]+ ), it matches any sequence of non-space characters followed by a single space. The modifyer {4} requires this construct to be matched exactly four times.

与 ([^ ]+ ) 中的尾随空格组合在一起,它匹配后跟单个空格的任何非空格字符序列。修改器 {4} 要求此构造精确匹配四次。

The single "r" is then the literal character you are searching for.

单个“r”就是您要搜索的文字字符。

In plain words this could be written like "If the line starts <^> with four strings that are followed by a space <([^ ]+ ){4}> and the next character is , then the line matches."

简而言之,这可以写成“如果该行以 <^> 开头的四个字符串后跟一个空格 <([^ ]+ ){4}> 并且下一个字符是 ,则该行匹配。”

A very good introduction into regular expressions has been written by Jan Goyvaerts (http://www.regular-expressions.info/quickstart.html).

Jan Goyvaerts (http://www.regular-expressions.info/quickstart.html) 写了一篇非常好的正则表达式介绍。

回答by Johnsyweb

grepcan handle this for you:

grep可以为您处理:

yourcommand | grep -- 'r-----'

It's often useful to save the (full) output to a file to analyse later. For this I use tee.

将(完整)输出保存到文件以供以后分析通常很有用。为此,我使用tee.

yourcommand | tee somefile | grep 'r-----'

If you want to find the line containing "-b----" a little later on without re-running yourcommand, you can just use:

如果您想稍后在不重新运行的情况下找到包含“-b----”的行yourcommand,您可以使用:

grep -- '-b----' somefile

No need for cathere!

cat这里不需要!

I recommend putting --after your call to grepsince your patterns contain minus-signs and if the minus-sign is at the beginning of the pattern, this would look like an option argument to greprather than a part of the pattern.

我建议--在您调用 to 之后放置,grep因为您的模式包含减号,并且如果减号位于模式的开头,则这看起来像是 to 的选项参数grep而不是模式的一部分。

回答by Anoop Kumar

Filtering by awk cmd in linux:-

在 linux 中通过 awk cmd 过滤:-

Firstly find the column for this cmd and store file2 :-

首先找到这个 cmd 的列并存储 file2 :-

awk '/Domain-0 0 15485 /' file1 >file2

awk '/Domain-0 0 15485 /' 文件 1 > 文件 2

Output:-

输出:-

Domain-0 0 15485 16 r----- 1779042.1

域-0 0 15485 16 r----- 1779042.1

after that awk cmd in file2:-

在文件 2 中的 awk cmd 之后:-

awk '{print $1,$2,$3,$4,"\n",$5,$6}' file2

awk '{打印 $1,$2,$3,$4,"\n",$5,$6}' file2

Final Output:-

最终输出:-

Domain-0 0 15485 16

域-0 0 15485 16

r----- 1779042.1

r----- 1779042.1