如何获取标准输出的特定部分?

How do I grab a specific section of a stdout?

我正在尝试获取刚刚插入的驱动器的 sda#

tail -f /var/log/messages | grep sda:

Returns: Mar 12 17:21:55 raspberrypi kernel: [ 1133.736632] sda: sda1

我想获取标准输出的 sda1 部分,我该怎么做?

我建议将其与 GNU grep 一起使用:

| grep -Po 'sd[a-z]+: \Ksd[a-z0-9]+$'

\K: This sequence resets the starting point of the reported match. Any previously matched characters are not included in the final matched sequence.


参见:The Stack Overflow Regular Expressions FAQ