Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The '-m' option just sets a maximum number of matches; if grep reaches that number of matches, then it will stop.
If you do it within a script, then the rest of the standard input will not be consumed; this means that you can continue on searching later in the script. For example, you could have a script which looped around, doing 'grep -m1' each time, until it got to the end of the input.
thanks neon, ok let me give one scenario, I've a file in which I've set of numbers, I want to display the numbers only till 10, how can I run the command if I'm saying cat <filename>| grep m5, it should display me all the values less then 5, correct..? but it is not, I know I'm not using it correct, can you let me know how should I use it.
The '-m' flag does not control the pattern being matched, it only controls how many matches happen before exiting. You still have to specify a pattern to match.
For example, if your file input.txt contained:
Code:
a
b
aaa
bbb
aa
bb
and you said
Code:
grep -m2 'a' <input.txt
then the grep will match all the lines with the character 'a', but the '-m2' will stop it after the first two matches. So the output will be:
oh okey got it, now is there any thing using grep or sort I can display the values from a file which is less then some n mumber
like I have a file numbers.txt, which contains number
4
8
10
15
3
2
so I want to display the output where number is less then 10 and the result will be
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.