GREP (mini manual)

G

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
 


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
 
Thakns for the guide, literally been Googling for hours about this :)
 
Thanks for the little guide there mate, should come in handy.

Will give it a try later on tonight when, thanks again.
 
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.
 
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
 
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
 
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".
 
e2b0a8f0a8d9bf1a95b3be479dcdc733575e56d2ce7266cad47b9b49586deede.jpg
 

Staff online

Members online


Latest posts

Top