Exclusive Grep

November 7th, 2006 liewsheng Posted in grep, Text Manipulation | Hits: 35160 | 11 Comments »

When you have ‘grep’ some keyword:

grep "keyword" filename

tons of lines and dozen of words you don’t want may show on the screen. What can you for this? Of cause is filter out the unwanted word ;p it is easy to ‘exclusive’ the unwanted keyword:

grep "keyword" filename | grep -v "non-keyword"

the ‘-v’ here will display those keyword which don’t match with “non-keyword”. For example, if you have a file, abc.txt, with this contents:

testing, 12345
#testing , this the wrong code
testing 3456
testing, 452345
testing, 652345
ates, 953645
btest 452345
#testing , this wrong code
#testing , this wrong code
#testing , this the wrong code

and you want to grep a “testing” keyword but not those staring with “#”:

grep testing abc.txt | grep -v #testing

and the result will be:

testing, 12345
testing 3456
testing, 452345
testing, 652345

11 Responses to “Exclusive Grep”

  1. [...] Using the file form previous examples (Exclusive Grep), output of command below: [...]

  2. [...] this will only display the line with matching word. Using the file contents in previous example, if this command enter: [...]

  3. manoj.devrari Says:

    please let me know that what we can search the any file through grep command using some keyword of that file.

    Manoj devrari

  4. manoj,
    You are asking to search for files, where you specified keywords as for file name?

    use command find or locate instead, checkout the examples at here

  5. Amit Agarwal Says:

    How to search for existance of two keywords in a file. -either with AND or “OR”.
    For example I want to search a line in a file containing both the keywords or maybe I want to grep lines that doesnt contain either of the keywords

    Thanks in advance

  6. Amit, refers to examples at

    use -v for inverse-match.

  7. please let me know how to search for a keyword in a folder

  8. If you mean all the files in the folder then you can use “*” to refer to all the files or any regex per your requirement.

  9. Why people still make use of to read news papers when in this
    technological world all is accessible on web?

  10. Every weekend i used to pay a visit this web site,
    as i want enjoyment, as this this site conations in fact
    pleasant funny stuff too.

    web page – Jackie -

  11. You can certainly see your enthusiasm within the work you write. The arena hopes for more passionate writers such as you who aren’t afraid to mention how they believe. All the time follow your heart.

Leave a Reply