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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grep and -L 1

Status
Not open for further replies.

HopeLessGuy

Technical User
Jun 9, 2003
10
US
I have a question. In Linux, grep -L would show the filenames of all files that didn't have the string I was searching for. Does SunOS have an equivilent to this? The closest I can see is -v, but that shows the entire body excluding the string and doesn't show the filenames(s).

Also, is there a method for grep to tell you which files contain a certain number of entries? For example, grep -c "String" *.boom would list how man lines that String is found in. Does grep have the flexibility to where I can set the number of entries that should be found(Or less) and have it output the filenames?
 
Not sure about the first question. However the second question you could try
#grep -c "String" *.boom |wc -l

#
wc is word count,-l is line count, also I think fgrep will show you the file name of the string you are searching for.

P.
 
None of the standard Solaris greps have that functionality, however you can wrap it in a little script for a similar effect:

[tt]for f in *
do
if ! grep yoursearchstring $f > /dev/null
then
echo $f
fi
done [/tt]

This works because grep returns 1 if it doesn't find a matching string in the file, or 0 if it does.

Similarly to do the second you could:

[tt]grep -c searchstring * | nawk -F: '$2 >= 5 { print $1 }'[/tt]

...where 5 is the minimum number of matches you want.

Annihilannic.
 
Thank you very much Annihilannic and bigdaddy01. Those examples were exactly what I had in mind. Again, thank you!!
 
One more thing, is there a way to search for more than one string, where as the condition must be met or else return false?

I've read using '|' - grep "Yo|Me" * is how to search for multiple strings...

But it seems to act as an OR operator and not AND (I've tried & and &&). Is there a argument that makes it return both strings or else return false? What I'm trying to do is make grep search for 2+ strings and if that returns true, it will search it for other strings to better determine the fix. But it mainly depends on the beginning statement which I can't to function.
 
Nevermind, found out already. Nothing bad about trial and error :p
 
How did you do it? As far as I know there is no "and" operator for (e)grep. In the past I have had to use two greps in the pipeline, or in your example something like egrep 'Yo.*Me|Me.*Yo'

Annihilannic.
 
I just found a way around it, probably very inefficient and time consuming :p But I just used an 'IF' statement, since for my case I needed to find files that contained both strings. As a result, I used a nested 'IF' statement.

if grep "stringhere" *.rcf *.txt #First String
then
if grep "stringhere" *.rcf *.txt #Second String
then
#SolutionCodeHere
else
#EscapeCode
fi
else
#EscapeCode
fi

That's the best I could think of to find 2+ strings in one file and get either a 0 or 1 response. That statment can go on and on searching for how many strings you have, but I think it's probably the most inefficient and slow. But it works :p

Oh yeah, got a question about this function you helped me with

grep -c searchstring * | nawk -F: '$2 >= 5 { print $1 }'

Is there a way I can manipulate the output? Because it'll print the file extensions as well, but I just want the filename (Filename is important, since it resolves to names). I tried doing -

grep -c searchstring * | nawk -F: '$2 >= 5 { print ${1%.txt|.doc} }'

As well as any other variations including, "${1%.txt|.doc} }', but awk doesn't seem to like it. I just want to eliminate the file extensions. I'm trying to maintain the name, but eliminate the .txt and .doc
 
This should do it.

[tt]grep -c searchstring * | nawk -F: '$2 >= 5 { sub("\.(doc|txt)$","",$1) ; print $1 }'[/tt]

The method you were using is a Korn shell feature, however the AWK script between the single apostrophes is not actually interpreted by the shell.

The sub() function is documented on the nawk man page.

Annihilannic.
 
Thank you Annihilannic! That code has helped me, again thanks for helping!! Back to more scripting I go :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top