I'm, trying to use grep with a ^ and $ at the same time but for some reason that does not work. If I type

grep '^s.*e' german.dic

I get a lots of words starting with s and having an e somewhere. A snippet from the output


So there is a word 'szintigraphische', which has an e at the end of the line. What I would expect now is that if I typed

grep '^s.*e$' german.dic

that I would at least get that result. However, the resultset is empty. How should I write a correct regexp to find all words starting with an s and ending with an e?

  • 3
    Are you sure the command did not work? I checked and the output was szintigraphische! – coffeMug Oct 27 '13 at 11:55
  • 1
    Ditto coffeMug. I literally cut n' pasted your snippet into a file, then cut and pasted your grep '^s.*e$' and it output 'szintigraphische' as it should. Maybe there is extra whitespace at the end of the line in file? – goldilocks Oct 27 '13 at 11:58
  • 2
    You can use this '^s.*e\s*$' to skip white spaces if there are any. \s* tells grep to ignore white spaces. – coffeMug Oct 27 '13 at 12:05
  • Yep, coffeMug, it was indeed whitespace ! Thanks for the help. – user50037 Oct 27 '13 at 12:13
  • @coffeMug - please write that up as an answer so it can be accepted and marked as resolved. – slm Oct 27 '13 at 14:35

Perhaps you have some white spaces on the end of the 3rd line. To skip those you can modify your command as follows:

grep '^s.*e\s*$' german.dic 

\s* tells grep to include white spaces (tab/space/cr/lf) if there are any at the end of the current line.

| improve this answer | |
  • Note that although \s is supported in grep (GNU grep 2.21 here), it's not documented, so using [[:space:]] would be safer. – Alois Mahdal Oct 10 '15 at 2:48

Your Answer

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