GREP (mini manual)

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

  1. gcawood

    gcawood Administrator Staff Member

    Messages:
    49
    Likes Received:
    19
    Trophy Points:
    0
    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	
    
    


    Tetractys likes this.
  2. Nathan Rich

    Nathan Rich New Member

    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0
    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 New Member

    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Thakns for the guide, literally been Googling for hours about this :)
  4. germs

    germs New Member

    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    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 New Member

    Messages:
    87
    Likes Received:
    3
    Trophy Points:
    0
    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 New Member

    Messages:
    2
    Likes Received:
    1
    Trophy Points:
    1
    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
    DevynCJohnson likes this.
  7. Videodrome

    Videodrome Active Member

    Messages:
    221
    Likes Received:
    85
    Trophy Points:
    28
    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 New Member

    Messages:
    2
    Likes Received:
    1
    Trophy Points:
    1
    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 Active Member

    Messages:
    221
    Likes Received:
    85
    Trophy Points:
    28
    lol try to double grep.

    dmesg | grep Wireless | grep Detected
    DevynCJohnson likes this.
  10. Videodrome

    Videodrome Active Member

    Messages:
    221
    Likes Received:
    85
    Trophy Points:
    28
  11. ZZs

    ZZs Active Member

    Messages:
    137
    Likes Received:
    50
    Trophy Points:
    28
    This one is kind of neat.
    watch grep \"cpu MHz\" /proc/cpuinfo
    DevynCJohnson likes this.
  12. DevynCJohnson

    DevynCJohnson Well-Known Member Staff Member Staff Writer

    Messages:
    1,379
    Likes Received:
    1,094
    Trophy Points:
    113
    This is one of the coolest command combinations I have ever seen. Thanks for sharing!
    Haider92 likes this.
  13. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,388
    Likes Received:
    454
    Trophy Points:
    83
    You never saw it before?
  14. DevynCJohnson

    DevynCJohnson Well-Known Member Staff Member Staff Writer

    Messages:
    1,379
    Likes Received:
    1,094
    Trophy Points:
    113
    No. I guess I have been living under a digital rock.;)
    Haider92 likes this.
  15. ZZs

    ZZs Active Member

    Messages:
    137
    Likes Received:
    50
    Trophy Points:
    28
  16. DevynCJohnson

    DevynCJohnson Well-Known Member Staff Member Staff Writer

    Messages:
    1,379
    Likes Received:
    1,094
    Trophy Points:
    113
    Haider92 likes this.

Share This Page