Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

I have certain questions regarding grep.

  1. Why does the following command match '<Hello'?

    $ grep -E "\<H" test
    Hello World
    <Hello
    H<ello
    
  2. What needs to be done to match '<Hello' only?

share|improve this question

marked as duplicate by Gilles, jasonwryan, rahmu, Renan, maxmackie Mar 6 '13 at 2:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
to match ' use grep \' inputfile –  Rahul Patil Mar 5 '13 at 7:43
1  
1st Q. is wrong , I think –  Rahul Patil Mar 5 '13 at 7:45
    
You should rephrase your question. Show the contents of your test file and simply tell us the number of the line you wish to match. There a lot of ambiguities in your question at the moment. –  Arnaud Meuret Mar 5 '13 at 9:36
    
@ArnaudMeuret Question edited. –  user3539 Mar 5 '13 at 13:32
    
@user3539 I guess you mean "<Hello" (no quotes) instead of "'<Hello'". Please clarify. –  l0b0 Mar 5 '13 at 16:32

3 Answers 3

up vote 1 down vote accepted

To prevent grep from interpreting a string specially (a regular expression), use -F (or --fixed-string):

$ cat test
one < two
Hello World
X<H
A <H A
I said: <Hello>
$ grep -F '<H' test
X<H
A <H A
I said: <Hello>

Remember to quote the search pattern properly, otherwise it may be interpreted badly by your shell. For example, if you ran grep -F <H test instead, the shell will try to open a file named "H" and use it to feed standard input ofgrep.grep` will search for the string "test" in that stream. The following commands are roughly equivalent to each other, but not to the above:

 grep -F <H test
 grep -F test <H         # location of `<H` does not matter
 grep -F H test
 cat H | grep -F test    # useless cat award

As for matching words only, have a look at the manual page grep(1):

   -w, --word-regexp
          Select  only those lines containing matches that form whole words.  The
          test is that the matching substring must either be at the beginning  of
          the  line, or preceded by a non-word constituent character.  Similarly,
          it must be either at the end of the line  or  followed  by  a  non-word
          constituent   character.    Word-constituent  characters  are  letters,
          digits, and the underscore.

Example usage (using the above test file):

$ grep -F -w '<H' test
A <H A

(-F is optional here as <H does not have a special meaning, but if you intent to extend this literal pattern, it may be useful then)

To match the beginning of a word, you do need regular expressions though:

$ grep -w '<H.*' test    # match words starting with `<H` followed by anything
A <H A
I said: <Hello>
share|improve this answer
    
The last one was I was looking for. I thought I could give grep '\<<' test to match < at beginning of each word. But it didnt work out. Any idea why it didnt work? –  user3539 Mar 6 '13 at 0:19
    
@user3539 possibly because < is not considered a word character. See the manual page under The Backslash Character and Special Expression –  Lekensteyn Mar 6 '13 at 9:57

Ctrl-V is your friend.

Ctrl-V tells the shell to treat the following character as a non-special character.

So, for example,if you have a file that has a backspace character in a line, and you want to grep to find that line, try

grep [Ctrl-V] [Backspace] filename

share|improve this answer
    
C-v is verbatim insert, not disabling of "special characters", so it will not help in this case. ' is a shell metacharacter, it is already verbatim, the shell interprets it after the fact. This will not work. –  Chris Down Mar 5 '13 at 7:55

< is not a special character in any grep. However, in GNU grep \< is special and means the beginning of word (so the zero-width boundary before Hello in all your input lines).

In all greps \ is special. It either can escape a special character to remove its special meaning (so it's matched literally) or add a special meaning to a character (that's typically used to introduce new operators without breaking existing scripts, another way is to use things that would otherwise be invalid like *? or (?) or for ANSI C escape sequences like \n, \t...

To remove the special meaning of \, like the others, you need another \.

So to match <Hello, you need:

grep -E '<Hello'

And to match \<Hello, you need:

grep -E '\\<Hello'

Note that both < and \ are special to the shell as well so need quoting for the shell as well, hence the single quotes above (\ is also special (to the shell) inside double quotes, though only in front of other special characters inside quotes like newline, double quote, backslash, dollar or backtick, so you'd nee grep -E "\\\<Hello" or grep -E "\\\\<Hello" to match \<Hello).

So that pattern matches the full line, add the -x option to grep:

grep -xE '<Hello'

would match only lines whose content it exactly "<Hello".

To match at the beginning of the line:

grep -E '^<Hello'

(would match "<Hello" and "<Hello world>", but not World <Hello.

To match <Hello not preceded by a non-blank character (my interpretation of your at the beginning of a word):

grep -E '(^|[[:blank:]])<Hello'

or with BRE:

grep '^\(.*[[:blank:]]\)\{0,1\}<Hello'
share|improve this answer
    
How to match < at beginning of word only? –  user3539 Mar 5 '13 at 10:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.