简体   繁体   English

dmesg命令将其管道传输到grep

[英]Dmesg command piping it to grep

I am running dmesg that contains info about devices and drives connected to the computer. 我正在运行dmesg,其中包含有关连接到计算机的设备和驱动器的信息。 the IDE drive shows up as "hd" followed by a letter starting at "a". IDE驱动器显示为“ hd”,后跟一个以“ a”开头的字母。 I need to use the grep command to find things that contain hd or sd followed by a letter. 我需要使用grep命令查找包含hd或sd后跟字母的内容。

I know it's something along the lines of this: 我知道这是遵循以下原则的:

dmesg | dmesg | grep ... but I'm having trouble writing the regex for it. grep ...但我在为其编写正则表达式时遇到了麻烦。 How do I do that? 我怎么做?

Try grep '[hs]d[az]' 试试grep '[hs]d[az]'

So, dmesg | grep '[hs]d[az]' 因此, dmesg | grep '[hs]d[az]' dmesg | grep '[hs]d[az]'

EDIT 编辑

As mentioned below, you can do LC_ALL=C ; dmesg | grep '[hs]d[az]' 如下所述,您可以执行LC_ALL=C ; dmesg | grep '[hs]d[az]' LC_ALL=C ; dmesg | grep '[hs]d[az]' LC_ALL=C ; dmesg | grep '[hs]d[az]' to make sure that your locale does not do wonky stuff with the [az] . LC_ALL=C ; dmesg | grep '[hs]d[az]'以确保您的语言环境不会对[az]

END EDIT 结束编辑

If you want to match only things that begin with that, use '^[hs]...' 如果您只想匹配以此开头的内容,请使用'^[hs]...'

The ^ matches the beginning of the line, so it would match: ^匹配行的开头,因此它将匹配:

hda1 is something hda1是什么

and it would not match 它不匹配

i found hda1 here... 我在这里找到了hda1 ...

The answer by @mikeb is good, he just has a spurious | @mikeb的回答很好,他只是一个虚假的| character in his character class [h|s] which is a mistake. 他的角色类别[h|s]中的角色,这是一个错误。

If you want to make sure that the string only consists of hda, hdb, hdc, sda, sdb, sdc etc with no other letters before of after that you can use "\\b" to match word boundaries. 如果要确保字符串仅包含hda,hdb,hdc,sda,sdb,sdc等,且之后没有其他字母,则可以使用“ \\ b”来匹配单词边界。

dmesg | grep '\b[hs]d[a-z]\b'

If you want to know about partitions, remove the \\b at the end and replace it with \\d (for digits): 如果要了解分区,请在末尾删除\\b ,然后将其替换为\\d (用于数字):

dmesg | grep '\b[hs]d[a-z]\d'

or just leave it out then it will match both drives and partitions: 或只保留它,则它将同时匹配驱动器和分区:

dmesg | grep '\b[hs]d[a-z]'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM