Search code examples
grepcuttail

How do I grab a specific section of a stdout?


I am trying to grab the sda# of a drive that was just inserted.

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

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

I would like to grab the sda1 part of the stdout, how would I do that?


Solution

  • I suggest to use this with 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.


    See: The Stack Overflow Regular Expressions FAQ