shell script search for one or more whitespace regular expression

I have a shell script where I run du -sh on a directory, and if I want to find all Mb files, I run this line in my script:

Mb)
      du -sh $DIR/* 2>/dev/null|grep -v [0-9][Mm]|sort -nr
      ;;
 
The problem I have with this line is that if there's files with other digits and M in the name, it matches those too, like this:

146M    /tmp/file4ZURF4
117M    /tmp/fileoVe4mF
85M     /tmp/filezN0eoA
84K     /tmp/fileT8mZQr.csv
84K     /tmp/file4MbmT3.csv

So I want to match the whitespace that occurs after the M in 146M.  But I can't find the exact regular expression to do this in a shell script.  Can someone pass this along?  Thanks!
texasreddogAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nasirbestCommented:
try this
      du -sh $DIR/* 2>/dev/null|grep -v [0-9][Mm][\s]|sort -nr
0
tty2Commented:
Combination '<digit>M<space>' may be met in the name of file, too...

Need to find all strings, which begin from digits. It is symbol '^' in regexp.
Also, there may be more then one digit and decimal point. We need to use symbol '*'.
Besides that, option '-v' (or --invert-match) inverts the sense of matching, to select non-matching lines (according to man grep).
So, the final expression:

du -sh $DIR/* | grep ^[.0-9]*[Mm] | sort -nr

'.' in expression '[.0-9]' means decimal point, may vary in different locale.
0
woolmilkporcCommented:
In GNU regex it should be

 du -sh $DIR/* 2>/dev/null | grep "[0-9][Mm]\>" | sort -nr

- Why "grep -v" ? Doesn't make sense to me, unless yo're trying to exclude all MB files

- What if a filename ends in e.g. ...9M ?

In this case I'd recommend

du -sh $DIR/* 2>/dev/null  | grep -E  "^[0-9.]+[Mm]\>" |sort -nr

wmp


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.