Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I recently decided that enough was enough -- I was going to learn to use grep fluently. It's been all of three hours and I'm already stumped by this toy problem.

I'm currently syncing a RAID5 array, the progress of which can be monitored by reading /proc/mdstat. The output of cat /proc/mdstat is shown below.

$ cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] 
md1 : active raid5 sda4[0] sdb4[1] sdc4[2]
      5858765824 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]
      [=============>.......]  resync = 67.3% (1972073120/2929382912) finish=205.7min speed=77537K/sec

md0 : active raid5 sda3[0] sdb3[1] sdc3[2]
      998400 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]

unused devices: <none>

For fun, I thought I would use watch to monitor /proc/mdstat in real time, pipe it's output into grep, and show only the estimated remaining time.

My approach is as follows:

watch cat /proc/mdstat | grep finish=\d+\.\d | grep \d+\.\d

I'm stumped as to why this produced no output. In fact, the first grep expression produces no output, even though it seems to work on Regex101.

What am I doing wrong?

share|improve this question
watch 'grep -oP "finish=\K[\d.]+" /proc/mdstat' – Stéphane Chazelas Jan 17 '14 at 22:53
up vote 2 down vote accepted
watch cat /proc/mdstat | grep -oE 'finish=[[:digit:]]+\.[[:digit:]]' | grep -oE '[[:digit:]]+\.[[:digit:]]'

If you really like the perl-style "\d" format and your grep supports perl-style regexes, then:

cat mdstat | grep -oP 'finish=\d+\.\d' | grep -oP '\d+\.\d'

where the "-P" option specifies perl-style regular expressions.

The "-o" option tells grep to display only the part of the line that matches the regular expression. This is what removes the unwanted text and allows us to return only the time remaining.

share|improve this answer
Great answer, thanks! I read about the -P flag in the meantime, but I wasn't aware of -o -- that's a good trick to keep in mind! – blz Jan 18 '14 at 10:32

You can use:

watch cat mdstatout | grep -oP 'finish\=\d+\.\d+' | cut -d= -f2


The -o tells grep to only output the matching text and not the whole line and -P lets you use perl regex which enables \d.

Also, I use cut to pull the digits out rather than another regex.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.