regexbashawkseddrbd

How to extract drbd status using grep/regex/cut/awk/sed etc


From my output from /proc/drbd I am trying to extract 'UpToDate/UpToDate' section of this output per device (0 and 1). I tried:

cat /proc/drbd  | grep ' 0:' | grep -Eo 'ds:(.*)'

But that gives me:

ds:UpToDate/UpToDate C r-----

That is not what I'm looking for (looking for getting the slot where UpToDate/UpToDate propagates) or basically a return of 'UpToDate/UpToDate'..Anyways, here is the output of /proc/drbd:

  0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
  1: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-----

Solution

  • You can use the following grep command:

    grep -o 'ds[^[:space:]]\+' /proc/drbd
    

    If you don't want the ds: in front, you can use grep in perl mode (if you have GNU grep):

    grep -oP 'ds:\K[^\s]+' /proc/drbd
    

    the \K clears everything which has been matched before - in this case ds:.

    If you don't have GNU grep, you can use awk:

    awk -F'[: ]' '{print $8}' /proc/drbd
    

    or sed:

    sed 's/.*ds:\([^[:space:]]\+\).*/\1/' /proc/drbd