Exclusive Grep

November 7th, 2006 liewsheng Posted in grep, Text Manipulation | Hits: 49139 | 21 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

21 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.

  12. After being diagnosed with herpes, believing that
    there is no cure for herpes, lots of people begin to feel annoyed, mad or sad and cope with a continuous fear of rejection.

  13. Stress is one of the significant factors that can begin viral break outs such as herpes.

  14. Ok, right off the bat, ignore getting your girlfriend from the bar, strip
    club, whore home, and so on

  15. A igreja como Israel no passado, tinha suas leis, ordenanças, símbolos e também outros.

  16. A group of researchers, led by researchers at The Wistar Institute, has actually found that an infection with
    herpes simplex virus 1 (HSV-1) triggers rearrangements in telomeres, small stretches of DNA that serve as safety ends to.

  17. Not just will Airbnb assist house displaced individuals, they prepare to disperse info
    on disasters and healing efforts through their networks They will also inform hosts making them
    as prepared as possible for a disaster circumstance.

  18. Com mel 100% procedente e óleo Suco de Gengibre amêndoas
    que ajuda a reparar cabelos castigados e também protege cabelo da secura.

  19. You have to understand ways to effectively influence the thoughts of your sweetheart and make her to feel extremely protected with you constantly.

  20. What a data of un-ambiguity and preserveness of precious know-how on the topic of unpredicted emotions.

  21. Quality article, will now link on my Skype group. Unrelated, but anyone
    suffering from diabetic complications here? If so, check out this great information I found.

Leave a Reply