?
Solved

Need  a help with grep command

Posted on 2005-04-22
11
Medium Priority
?
393 Views
Last Modified: 2013-12-15
I am having some 50 files where each file has 10 lines.one particular
string is present in some files where it is not there in other files.
I am trying to find the name of the files where the string is not present
and move the the output to a file for further processing.
    I tried the following command
     
        grep -l -v 'string' filename>test

   But it is still showing the files where the string in present.

Can any one help me to sort out the problem?
0
Comment
Question by:srps1579
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 2

Expert Comment

by:nikkilocke
ID: 13842975
Hi srps1579,

grep -v will print all the lines from the file which don't match the supplied pattern.
grep -l will just list the filename of all files which would have printed one or more lines.
So your -l -v switches will print the filename of the file, unless ALL the lines in it match the pattern.

What you need to do is to search for the pattern (grep -l 'string' filename), and test the result code from the grep - grep will show a false result if no lines matched the pattern.

Something like

for filename in *.html; do grep -l 'order' $filename || echo $filename >> test; done

This will echo to the screen all the files containing the word order, but write to the file test all the files not containing the word order.

Hope this helps,

Nikki
0
 

Author Comment

by:srps1579
ID: 13843139
It doesn't works nikki
0
 
LVL 2

Expert Comment

by:nikkilocke
ID: 13843214
Interesting - it works here (I tried it).
What happens?
What output do you get?
What do you get in the test file?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:srps1579
ID: 13844792
The test file contains all the files.and also the files containing the word are not displayed in the screen
0
 
LVL 2

Expert Comment

by:nikkilocke
ID: 13845090
Any chance you could post two or three short sample files, and the word you are searching for?
It may be that creating the sample files, and testing them, will reveal to you what the problem is.
If not, I can put them on my Linux box, and test them myself.

Nikki
0
 
LVL 7

Expert Comment

by:macker-
ID: 13846662
While testing the result code is an interesting approach, it's not the simplest.

grep -L "pattern_I_do_not_want" *filemask* > test

The -L switch will scan files for the pattern you specify, and if it finds the pattern, it will skip on to the next file.  If it doesn't find the pattern by the end of the file, it will print the file name, and continue on to the next file.

Please note that this is case sensitive.. you must use the upper-case L.  This was based on GNU grep 2.4.2, but other versions of GNU grep should be fine.  If it's not GNU grep, or a very dated version, available options may differ.
0
 

Author Comment

by:srps1579
ID: 13848930
Nikki,
 
FYI
     File1 contains

        mailx -s success srp156@yahoo.com brick@yahoo.com bell@gmail.com jack@yahoo.com

     File2 contains
         mailx -s success srps1579@yahoo.com  nancy@yahoo.com merlin@yahoo.com

     grep -l -v nancy File*>test            --->this does'nt works

 Now I am searching for the pattern nancy in all the File* ,It should give me the file name i.e File1 only and also I need to pass this to another file.

    I am not having -L option in grep .Probably mine is a older version

       
0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 13851143
How about egrep or fgrep?
egrep -L nancy File* >test
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13852864
if your grep does not support -L (which is unusal 'cause Linux uses GNU grep):

(grep -l 'string' list of files ; ls -1 list of files)|sort|uniq -d
0
 
LVL 7

Expert Comment

by:macker-
ID: 13854482
I would strongly consider upgrading the version of grep installed.  There are work-arounds, such as the one ahoffman suggested, but upgrading grep will yield far more simplicity in the long-run.  The exception would be if you needed to reproduce this across a large network of systems that may not have an upgraded grep.
0
 
LVL 2

Accepted Solution

by:
nikkilocke earned 500 total points
ID: 13856957
I tested this on my machine, and it works. I think your problem is you didn't do the right command - you need the for loop and everything like I suggested...

[nikki@redhat nikki]$ cat f1.txt
        mailx -s success srp156@yahoo.com brick@yahoo.com bell@gmail.com jack@yahoo.com
[nikki@redhat nikki]$ cat f2.txt
         mailx -s success srps1579@yahoo.com  nancy@yahoo.com merlin@yahoo.com
[nikki@redhat nikki]$ for filename in f*.txt ; do grep -l nancy $filename || echo $filename >>test; done
f2.txt
[nikki@redhat nikki]$ cat test
f1.txt
[nikki@redhat nikki]$

Of course, grep -L would have been easier, if you had had the -L option...

[nikki@redhat nikki]$ grep -L nancy f*.txt
f1.txt
[nikki@redhat nikki]$

[Sorry about the delay in replying - I'm trying not to be a nerd, so don't turn the computer on at weekends if I can help it!]
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question