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 MeuretMar 5 '13 at 9:36

3 Answers
3

$ 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:

-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>

< 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):

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 DownMar 5 '13 at 7:55