LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 11-23-2011, 01:31 AM   #1
rajsierra
LQ Newbie
 
Registered: Jan 2009
Location: Hyderabad, India
Posts: 13

Rep: Reputation: 0
Smile use of grep -m NUM


I know its a very simple question, but can you help me on using the -m option with grep to display the content of file once it reach NUM
 
Old 11-23-2011, 01:49 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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.
 
Old 11-23-2011, 02:37 AM   #3
rajsierra
LQ Newbie
 
Registered: Jan 2009
Location: Hyderabad, India
Posts: 13

Original Poster
Rep: Reputation: 0
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.
 
Old 11-23-2011, 02:43 AM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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:
Code:
a
aaa
 
Old 11-23-2011, 04:11 AM   #5
rajsierra
LQ Newbie
 
Registered: Jan 2009
Location: Hyderabad, India
Posts: 13

Original Poster
Rep: Reputation: 0
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

4
8
3
2
 
Old 11-23-2011, 04:52 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Try awk:
Code:
awk '$1 < 10' file
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how can grep from file (num num) (num num) ? DoME69 Programming 2 11-07-2011 06:56 AM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
Num Lock? RIG12 Programming 2 07-02-2007 05:58 PM
Help with NUM GSX Linux - Newbie 6 07-07-2005 12:30 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration