Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grep for string in multiple files only one instance.

Status
Not open for further replies.

galger

MIS
Jan 16, 2002
79
US
if multiple instances exsist in multiple files.. How can I return only one instance.. I did this a long time ago.. looked through man page. could not find anything.. let know.


Thanks in advance.
galger.

 
Something like this ?
grep -q "$string" list of files && echo "$string found"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
And this ?
grep "$string" list of files | head -1

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

No idea if is something standard, or just a GNU extension :
man grep said:
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines.
By the way. I use [tt]grep (GNU grep) 2.5.1[/tt]. Is any official name of that "solaris grep" ? I would like to google for it to read some details.

Feherke.
 
man grep on a Solaris box should give you the vanilla grep man page (I think).
 
-q does not work for solaris grep
Courtesy of Annihilannic I see you have to use this version of grep:
/usr/xpg4/bin/grep

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Not sure what you mean? Before GNU grep, grep was just grep.

The xpg4 one is the POSIX compliant version I believe.

Annihilannic.
 
I guess it's compliant with:
ISO/IEC DIS 9945 - 2:1992, Information technology - Portable
Operating System Interface (POSIX) - Part 2: Shell and Utilities
(IEEE Std 1003.2 - 1992);

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Galger,

this is not exactly what you asked for in your original post, but I'm wondering if what you are really after is just a list of the files that contain any occurrence of your search string. If so, try the -l switch for grep:

Code:
grep [b]-l[/b] "search-string" <file-list>

I'm on SCO, so I hope the "l" switch is available on Solaris.

Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
# You can also
grep SomeString files* | uniq
# to return just one instance
 
kordaff, in which flavor of *nix does uniq return just one row of an unsorted file ?
 
It depends on your search string of course:

[phil@localhost phil]$ grep alpha text* | uniq
text:alpha
text2:alpha
text3:alpha just a word on a line?
text3:alpha

[phil@localhost phil]$ perl -e 'for(1..$#ARGV){open(FILE,$ARGV[$_])or die"$!\n";while(<FILE>){if (/$ARGV[0]/){$flag=1}}}if ($flag){print "Found $ARGV[0]\n"}' alpha text text2 text3

Found alpha

Perl does a better job if you need to search for a term that is part of a line in multiple files...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top