Exclusive Grep

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

8 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:

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

    Regards’
    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
    Amit

  6. Amit, refers to examples at
    http://linux.byexamples.com/archives/290/grep-multiple-keywords-and-not-or/

    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.

Leave a Reply