4

I need to find a line in a file that starts with an a and the last word in the line ends with an e.

How can I do it with a tool like grep?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
PFKrang
  • 229
  • 2
  • 7
  • 17
  • 1
    What is the question here? What the regex should be? You should consult a regex howto to learn that stuff... – arkascha Mar 06 '15 at 13:04

3 Answers3

18

Just say this:

grep '^a.*e$' file

This means: look for those lines starting (^) with a, then 0 or more characters and finally and e at the end of the line ($).

Test

$ cat a
hello
and thisfinishes with e
foo
$ grep '^a.*e$' a
and thisfinishes with e
fedorqui
  • 275,237
  • 103
  • 548
  • 598
2

Simple answer : use grep.

grep -E "^a.*e$" filename

the ^ indicates the beggining of the line the $ marks the end of the line the .* means any character (the .) repeated from zero to any number of times (the *). Many topics have already answered this questions, like this one. If you want to know more of searching, you could look more in depth into REGEX.

Community
  • 1
  • 1
Aulo
  • 141
  • 2
  • 7
0
$ grep -E '.*sam.*t' filename

here .* is used for any character that regex can find. here "sam" is the word in my case.

jun41D
  • 3
  • 2