0

Im trying to find a line that contains the word ell in a text file, however when I use the grep command my result is Well I don?t knowthat?er 10, 2016. I also converted the text file using the dos2unix name_of_file.txt command but the issue still lingers. What am I doing wrong? Please let me know.

Here is my script:

#!/bin/sh
if [ ! -f $2 ]
then
    echo The file does not exist
    exit
else
    grep $1 $2
fi

Here is my text file:

Hello today is September 10, 2016.
NYC.
What the hell is that?
London.
Well I don’t know.

The result that I am expecting is:

Hello today is September 10, 2016.
What the hell is that?
Well I don’t know.
  • Can you view the line breaks to see what characters are really in the file? It has to be an issue with the line break characters that's interfering with the way grep is outputting the lines. – Brian Stephens Sep 11 '16 at 02:29
  • yes, all the lines end with ^M @BrianStephens – Bthegreatest Sep 11 '16 at 02:32
  • You may want to read this, about converting ^M characters to \r: http://stackoverflow.com/questions/811193/how-to-convert-the-m-linebreak-to-normal-linebreak-in-a-file-opened-in-vim – Brian Stephens Sep 11 '16 at 02:37

1 Answers1

0

The issue was with the format of the text file. I saved the text document using Microsoft Word in a .txt format, however, its default option does not save it to MS-DOS type, either select that type, ASCII, or Western(Text) to resolve this problem.