0

I want grep all lines containing words that start with "H" and end with "d". How would i do that? I found that grep ^H will give me the H for everyline.

migrated from stackoverflow.com Nov 4 '11 at 11:14

This question came from our site for professional and enthusiast programmers.

2

I would try: /^H.*d$/. The ^ is the starts with and $ end with. The .* matches anything.

  • sweet thanks man! I had to take off the "/" but that worked thanks again – aaron aaron Nov 3 '11 at 22:38
2

How do you define "words"?

If they're delimited by spaces:

grep '\<H[^ ]*d\>' ...

should do it. \< and \> match the beginning and end of a word, respectively. [^ ]* matches a sequence of zero or more non-space characters.

More, generally, if your grep supports it (GNU grep does):

grep '\<H[^[:space:]*d\>

finds words delimited by "space" characters: tab, newline, vertical tab, form feed, carriage return, and space.

I'm assuming that "lines containing word" means that you want "foo H123d bar" to match. If each line contains an entire word, replace \< by ^ and \> by $.

EDIT: This might not be 100% correct. \< and \> define a "word" as a non-empty sequence of letters, digits, and underscores. Define exactly what you mean by "word", and I'll try to refine my answer. In "*H*d*" is H*d a "word"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy