1

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?

Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
Tony
  • 716
  • 9
  • 15

3 Answers3

2

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}' *
Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
  • 1
    Nice approach. There would be a problem if the result is a single file, since then there is no filename with a colon in the output. You could use `/dev/null` similar to like I posted here: http://stackoverflow.com/questions/15432156/display-filename-and-match-in-grep/15432718#15432718 and use it as the first argument and print `NR>1` in `awk` – Scrutinizer Apr 01 '14 at 15:20
  • @Scrutinizer Thanks for your input, very valid point! I adapted my answer but realized that this will also print directories which is certainly not desired :( – Adrian Frühwirth Apr 02 '14 at 08:49
0

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
Farvardin
  • 5,336
  • 5
  • 33
  • 54
-1

You can use awk like this:

awk '!/not this/' file

To do multiple not:

awk '!/jan|feb|mars/' file
Jotne
  • 40,548
  • 12
  • 51
  • 55