Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I searched a lot over google and stackoverflow/serverfault, but I was not able to find a corresponding to "-m" option in the bash shell. I need to fetch only the first result with the grep. Is this possible in the sun-os/solaris?

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

There is no -m option to the Solaris grep. The -m N switch stops reading a file after N lines have been matched. If all you want is N lines of output then you could use

grep test file | head -N 

where N is the number of lines of output that you want.

share|improve this answer
1  
If you have the SUNWggrp package installed then you can use GNU grep from /usr/sfw/bin/ggrep. –  James O'Gorman Feb 13 '13 at 17:09
    
@JamesO'Gorman: ggrep - no wonder I couldn't find it :( –  Iain Feb 13 '13 at 17:30
    
There is also CSWgrep from OpenCSW. While it installs grep as /opt/csw/bin/ggrep, you also have the /opt/csw/gnu directory with symlinks, using the original names. –  automatthias Feb 13 '13 at 21:06
add comment

You would need GNU grep to use the -m option. You could use awk or sed in Solaris instead, if you do not want to use GNU grep:

awk '$0 ~ var{print;exit}' var=$string mytextfile
share|improve this answer
2  
On Solaris 10 you need to use nawk to get this to work. –  Iain Feb 13 '13 at 14:08
add comment

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.