Tony
Tony

Reputation: 816

Solaris: find files not containing a string (alternative to grep -L)

I want to search files that does not contain a specific string.

I used -lv but this was a huge mistake because it was returning all the files that contain any line not containing my string.

I knew what I need exactly is grep -L, however, Solaris grep does not implement this feature.

What is the alternative, if any?

Upvotes: 1

Views: 1632

Answers (3)

Adrian Frühwirth
Adrian Frühwirth

Reputation: 45576

You can exploit grep -c and do the following (thanks @Scrutinizer for the /dev/null hint):

grep -c foo /dev/null * 2>/dev/null | awk -F: 'NR>1&&!$2{print $1}'

This will unfortunately also print directories (if * expands to any) which might not be desired in which case a simple loop, albeit slower, might be your best bet:

for file in *; do
    [ -f "${file}" ] || continue
    grep -q foo "${file}" 2>/dev/null || echo "${file}"
done

However, if you have GNU awk 4 on your system you can do:

awk 'BEGINFILE{f=0} /foo/{f=1} ENDFILE{if(!f)print FILENAME}' *

Upvotes: 2

Farvardin
Farvardin

Reputation: 5424

after using grep -c u can use grep again to find your desired filenames:

grep -c 'pattern' *  | grep  ':0$'

and to see just filnames :

grep -c 'pattern' *  | grep  ':0$' | cut -d":" -f1

Upvotes: 0

Jotne
Jotne

Reputation: 41456

You can use awk like this:

awk '!/not this/' file

To do multiple not:

awk '!/jan|feb|mars/' file

Upvotes: -1

Related Questions