3

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.

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

2 Answers 2

6

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.

2
  • Why the backslash? Commented Feb 16, 2015 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
    Commented Feb 16, 2015 at 12:45
0

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"'$'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .