1

I'm searching a file which should not have this string "section" using shell/perl.

Please help me to find way grep -vl command returns a file names if strings exists.

user2347191
  • 99
  • 1
  • 2
  • 14
  • 2
    Possible Duplicate: http://stackoverflow.com/questions/4749330/how-to-test-if-string-exists-in-file-with-bash-shell – Zero Fiber Oct 20 '13 at 06:20

3 Answers3

5

You may try like this:-

grep -Fxq "$MYFILENAME" file.txt

or may be like this:-

if grep -q SearchString "$File"; then
   Do Some Actions
fi
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 3
    Did you just copy the answer from the duplicate and put it here? – beroe Oct 23 '13 at 00:18
  • The `if` statement would have been my answer too -- even before looking at the duplicate question. It's just the standard way. However, I'd add the `-F` and `-w` parameters to my `grep`. The `-F` turns off regular expressions, and the `-w` only selects against a fixed word. – David W. Mar 03 '15 at 15:52
4

In perl:

    open(FILE,"<your file>");
    if (grep{/<your keyword>/} <FILE>){
       print "found\n";
    }else{
       print "word not found\n";
    }
    close FILE;
Omer Dagan
  • 14,868
  • 16
  • 44
  • 60
-1

A lot of times I'll do it this way:

for f in <your_files>; do grep -q "section" $f && echo $f || continue; done

That should print out a list of files that contain the word "section" in them.

drmrgd
  • 733
  • 5
  • 17