Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
    
You can't use \d and always quote a regex inside grep. See ‘grep’ regular expression syntax. –  Radu Rădeanu Jan 17 at 21:45

3 Answers 3

up vote 2 down vote accepted

If you want to use regex syntax you need -P switch with grep. Check out previously asked guestion here Is grep syntax different from regex?

share|improve this answer
    
Great answer -- concise, accurate, and with additional references to boot! Thank you! –  blz Jan 18 at 10:29
    
You are welcome. –  kenn Jan 18 at 10:42
  • You should quote your expression so the shell doesn't interpret it
  • grep doesn't have the \d escape, you'll need to use [0-9] instead.
  • + needs to be escaped without the -E switch.

This should work:

watch cat /proc/mdstat | grep 'finish=[0-9]\+\.[0-9]' | grep '[0-9]\+\.[0-9]'
share|improve this answer
    
I accepted kenn's answer because the -P switch will allow me to use regexes in a way that I'm already quite comfortable with. I'm still upvoting your answer, though, because it's a nice solution. I'll keep this in mind! Thanks! –  blz Jan 18 at 10:31
1  
@blz The grep supports 3 'styles' of regular expressions: "basic" -G (default), "extended" -E and "perl" -P. The basic mode requires that ?, +, {, |, (, and ) be escaped to have special meaning. –  minerz029 Jan 18 at 20:36

Use sed instead, this worked for me:

watch "cat /proc/mdstat | grep 'finish\=' | sed -e 's/.*finish\=\([0-9,\.]*\).*/\1/g'"
share|improve this answer
    
The OP wants "to learn to use grep fluently." –  Radu Rădeanu Jan 17 at 21:56
    
@chaos, Didn't know about sed. I'm making a mental note of this =) –  blz Jan 18 at 10:30

Your Answer

 
discard

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.