2

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

szientistischer
szientistisches
szintigraphische
szintigraphischem
szintigraphischen
szintigraphischer
szintigraphisches

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?

4
  • 3
    Are you sure the command did not work? I checked and the output was szintigraphische!
    – Vombat
    Commented Oct 27, 2013 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
    Commented Oct 27, 2013 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.
    – Vombat
    Commented Oct 27, 2013 at 12:05
  • Yep, coffeMug, it was indeed whitespace ! Thanks for the help.
    – user50037
    Commented Oct 27, 2013 at 12:13

1 Answer 1

4

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.

2
  • Note that although \s is supported in grep (GNU grep 2.21 here), it's not documented, so using [[:space:]] would be safer. Commented Oct 10, 2015 at 2:48
  • It is likely that the file they are working with (were, 7+ years ago) was a Windows/DOS text file with carriage return characters at the end of each line. The correct course of action would then be to convert the german.dic file to a Unix text file using dos2unix.
    – Kusalananda
    Commented Jan 8, 2021 at 16:26

You must log in to answer this question.