Wednesday, January 13, 2016

grep examples


Using grep with a "." or dot in the search term (copy because of overactive editors)

http://stackoverflow.com/questions/10346816/using-grep-to-search-for-a-string-0-49

                     
I am trying to search for a string 0.49 (with dot) using the command
grep -r "0.49" *
But what happening is that I am also getting unwanted results which contains the string such as 0449, 0949 etc,. The thing is linux considering dot(.) as any character and bringing out all the results. But I want to get the result only for "0.49".










grep uses regexes; . means "any character" in a regex. If you want a literal string, use grep -F, fgrep, or escape the . to \..

grep -F -r '0.49' * treats 0.49 as a "fixed" string instead of a regular expression. This makes . lose its special meaning.

Using grep escaping - (dash) characters

Quote the grep string with double quotes.  escape out the - with a \ (backslash) or any other specials.

Example:

ls -l | grep "\-\-\-\-" 

used to locate areas in an ls where there are no permissions.  the --- for plus the special for four dashes will appear in an ls status with an area with no access.

works with a find -ls command as well for an entire subtree:

find . -ls | grep "\-\-\-\-"

--30--

No comments:

Post a Comment