?
Solved

shell script search for one or more whitespace regular expression

Posted on 2010-04-01
3
Medium Priority
?
380 Views
Last Modified: 2013-12-26
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!
0
Comment
Question by:texasreddog
3 Comments
 
LVL 6

Expert Comment

by:nasirbest
ID: 29378980
try this
      du -sh $DIR/* 2>/dev/null|grep -v [0-9][Mm][\s]|sort -nr
0
 
LVL 6

Expert Comment

by:tty2
ID: 29392754
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
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 29395024
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

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question