Purchase Linux CDs / DVDs / Flash Drives at OSDisc.com

Welcome to Our Community

While Linux.org has been around for a while, we recently changed management and had to purge most of the content (including users). If you signed up before April 23rd please sign up again. Thanks!

GREP (mini manual)

Discussion in 'General Linux' started by gcawood, Oct 29, 2011.

  1. gcawood

    gcawood Guest

    grep is such a powerful little tool that the number of ways to use it are really limitless. However, if you are just starting out, try to put these into your head..

    Code:
    # find all occurrences of word in a specified file
    grep word file			
    
    #exclude word from result
    grep –v word
    
    #return the line and the next 2 lines after it.
    grep –A 2 word file 		
    
    #match only one occurrence of word
    grep word | head -1			
    
    #repeat any character any number of times
    grep .*			
    
    #whole word match
    grep -w 'word' *		
    
    #To find 1.gif in a file use, use \ to get around reg-x
    grep '1\.gif' file		
    
    #Searches for word in all file recursively in the directory /DIR and the /dev/null shows the file name
    grep -r word /DIR /dev/null	
    
    



     
    1 person likes this.
  2. Nathan Rich

    Nathan Rich Guest

    Code:
    #match only one occurrence of word
    grep . word			
    
    #match one or more occurrences of word
    grep * word	
    These are wrong completely.

    Code:
    #whole word match
    grep -w 'word' *
    should say

    Code:
    #whole word match
    grep -w word

    Code:
    #repeat any character any number of times
    grep .*	
    You will have a bad experience if you try this grep, especially in your home directory


    Code:
    #To find 1.gif in a file use, use \ to get around reg-x
    grep '1\.gif' file
    Neither backslash nor single quotes have anything to do with regex. Regex is applied to grep. That's what the re means. The single quotes stop BASH from going into expansion for that IFS-separated block of characters. Backslash does that only for the next character. However the regex itself needs a backslash as well. So the quoting stops the removal of the backslash by BASH and the backslash itself is part of regex specifications. You can also do this, therefore:

    Code:
    #To find 1.gif in a file use, use \ to get around expansion
    grep 1\\.gif file
     
  3. Jay

    Jay Guest

    Thakns for the guide, literally been Googling for hours about this :)
     
  4. germs

    germs Guest

    Thanks for the little guide there mate, should come in handy.

    Will give it a try later on tonight when, thanks again.
     
  5. MustangV10

    MustangV10 Guest

    Thanks for sharing this. I haven't really used GREP myself, I knew it existed, but never really got to grips with what it actually did and it's practises.
     
  6. StratoMech

    StratoMech Guest

    I have a more defined problem. I am searching a dictionary file for a specific sequence. My current grep command looks like this:

    grep -w -E '^[dD][aeiouy][a-z][a-z][s$]' dictionary.file

    where "dictionary.file" is just the path name to the dictionary file.

    The objective is to find all 5-letter words in the dictionary file that start with "d", are followed by a vowel, and end with an "s". It works with the exception of one word, which ends in an "s's".

    After three days, I have been unable to get rid of the ending apostrophe and "s". Does anyone have any ideas? [^'] does not work, as I receive an "Unmatched '." error

    Thanks in advance.
    -John
     
    1 person likes this.
  7. Videodrome

    Videodrome Guest

    It can be good for searching or filtering searching. Poking into /var/logs for specific things for example.

    I mostly use it to filter the long output of commands like dmesg or ps -e.

    #ps -e | grep chromium
     
  8. StratoMech

    StratoMech Guest

    That's what I am doing. I have no problem searching for strings that I can define. For instance, if I use grep on the /var/log/messages file, I can tell someone has been trying to brute-force their way to the root password by searching for "Failed password for root".

    As a loose example, my issue is that if I was looking for "password" only, and there was a misspelled "password'd", I have not been able to just get "password" to come up using something like the code above. In the case above, "password" and "password'd" would both come up. I need to figure out how to drop the "'d".
     
  9. Videodrome

    Videodrome Guest

    lol try to double grep.

    dmesg | grep Wireless | grep Detected
     
    1 person likes this.
  10. Videodrome

    Videodrome Guest

    [​IMG]
     
    4 people like this.
  11. ZZs

    ZZs Guest

    This one is kind of neat.
    watch grep \"cpu MHz\" /proc/cpuinfo
     
    1 person likes this.
  12. This is one of the coolest command combinations I have ever seen. Thanks for sharing!
     
    1 person likes this.
  13. ryanvade

    ryanvade Guest

    You never saw it before?
     
  14. No. I guess I have been living under a digital rock.;)
     
    1 person likes this.
  15. ZZs

    ZZs Guest

  16. 1 person likes this.

Share This Page