Find lines starting with one specific character and ending with another one

梦想与她 提交于 2019-12-10 11:07:04

问题


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?


回答1:


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



回答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.



来源:https://stackoverflow.com/questions/28899349/find-lines-starting-with-one-specific-character-and-ending-with-another-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!