I know that to find a REGEX that ends in a certain string I have to write "string$". Although I tried the following variations of grep : cat foo | grep "string$", cat foo | egrep "string$", grep -E "string$", grep "string\$" I did not get any found lines although such line exists.

  • Please give example of such line which doesn't work. – jimmij Feb 16 '15 at 12:26
  • Can you put an example of such lines and the full command you have used? – jcbermu Feb 16 '15 at 12:26
  • I verbatim typed in my shell: cat soi | grep "ing$" (and the variations of it) – Abdul Al Hazred Feb 16 '15 at 12:29
  • 3
    Do those files have dos style line endings ? – Leiaz Feb 16 '15 at 12:31
  • I do not know what dos style line endings are , but for instance cat soi | grep "^I" works. – Abdul Al Hazred Feb 16 '15 at 12:32

This should work:

grep 'ing$' soi

catis not needed. Try with single quotes.

Single quotes prevent the shell from interpreting $ as the beginning of a variable name.

  • Why the backslash? – Scott Feb 16 '15 at 12:43
  • An error. Corrected. You could need the backslash if you are looking for for lines that end in .ing, so the backslash escapes the dot. – jcbermu Feb 16 '15 at 12:45

Use single quotes to make the $ work as end-of-line.

If you want to grep with some variable also, use both double and single quotes as below:

grep -- "$var"'$'

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.