1

I have a text file with 4 sentences and I want to grep the ones ending in "me", however when I do grep "me$" it doesn't work.

Am I missing something?

(I wanted the result to be the 1st, 2nd and 4th line).

Edit: Replaced image with text.

$ cat sm.txt
somebody once told me

somebodyoncetoldme

the world was

gonna roll me
$ grep "me$" sm.txt
$
5
  • The extra spacing between the lines in the cat output is strange, and the spaces before the prompt string are even stranger. Does file sm.txt tell you it is a plain ASCII file or it it reporting DOS-style line ends?
    – xenoid
    Commented Oct 31, 2018 at 10:08
  • @rropes, What do you see with cat -e sm.txt and/or od -c sm.txt Commented Oct 31, 2018 at 17:21
  • Also, grep knows how to read files, so you should grep 'me$' sm.txt Commented Oct 31, 2018 at 17:22
  • @glennjackman I used cat -e sm.txt and this showed up: somebody once told me^M$ ^M$ somebodyoncetoldme^M$ ^M$ the world was^M$ ^M$ gonna roll me^M$
    – rropes
    Commented Nov 1, 2018 at 17:36
  • @xenoid it says: sm.txt: ASCII text, with CRLF line terminators
    – rropes
    Commented Nov 1, 2018 at 17:38

2 Answers 2

1

In comments you say that file identifies the text file as an ASCII text file with CRLF line endings. That's another way of saying it's a DOS text file, possibly generated with an editor on a Windows system.

A DOS text file has, in comparison to a Unix text file, an extra carriage return character at the end of each line, before the newline. This is why your regular expression does not match, no line actually ends with me, they end with me\r, where \r is a common way of writing a carriage return character.

The remedy for this is to convert your text file to a Unix text file using e.g. the dos2unix conversion tool, before using it on a Unix system.

2
  • Alternately, search for lines ending the "me" followed by any whitespace: grep 'me[[:space:]]*$' file Commented Nov 1, 2018 at 23:17
  • Thank you, I tried doing grep "me\r$" but that didn't work, and after that I used the dos2unix conversion tool and did the normal grep "me$" and it worked perfectly.
    – rropes
    Commented Nov 2, 2018 at 17:31
0

In itself grep "me$" definitely is correct. I suspect your lines end with a few additional characters that are hard to see, like tabs and spaces.

Try one of these:

# \t means tab
# [\t ] means a tab or a space
# [\t ]* means any number (including zero) of tabs and spaces.
#
grep "me[\t ]*$" sm.txt

Or:

# [[:space:]] means any white space. I don't know if there are
# more than just space and tabs. This works with GNU grep only.
#
grep "me[[:space:]]*$" sm.txt
2
  • 4
    [[:space:]] is more than just a space or a tab (see man isspace). [[:blank:]] is only a space or a tab (see man isblank).
    – Kusalananda
    Commented Oct 31, 2018 at 9:56
  • I tried both but this: $ grep "me[\t ]*$" sm.txt $ grep "me[[:space:]]*$" sm.txt $ (image since I don't know how to do paragraphs in comments) imgur.com/a/IuQoHSX
    – rropes
    Commented Nov 1, 2018 at 17:55

You must log in to answer this question.

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