Results 1 to 5 of 5

Thread: grep returns odd results based on EOL

  1. #1
    Join Date
    Jun 2008
    Beans
    30

    Cool grep returns odd results based on EOL

    I'm doing some file manipulation which requires using grep to handle files. For some reason, grep returns strange results with Mac format text end of line (EOL) characters (CR). Grep works okay for me as long as the end of line is given with a line feed (LF), but not with carriage returns only.

    The idea is this:

    ThisIsMySearchString
    THISISMYTESTINGSTRING

    if I use grep to locate the string 'Search', it should return the name of the file and the line in which it is found. Instead, the CR file (testing2) returns

    THISISMYTESTINGSTRINGchString

    in the command line interface.
    What's more, when I use redirection to output the results of the command, I GET A DIFFERENT RESULT!

    Any help would be appreciated.
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    6,590
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: grep returns odd results based on EOL

    Hi thatsmyboy.

    When you are doing this:
    Code:
    $ grep This testing2.txt 
    THISISMYTESTINGSTRING
    The whole file is being printed because grep doesn't recognize 'cr' as line endings. You don't see the whole content because you are printing crs the lines get overwritten.

    You can see what is going on if you do:
    Code:
    $ grep This testing2.txt | od -a
    0000000   T   h   i   s   I   s   M   y   S   e   a   r   c   h   S   t
    0000020   r   i   n   g  cr   T   H   I   S   I   S   M   Y   T   E   S
    0000040   T   I   N   G   S   T   R   I   N   G  cr  nl
    0000054
    I hope that points you in the right direction.
    Regards.

  3. #3
    Join Date
    Aug 2006
    Beans
    252

    Re: grep returns odd results based on EOL

    worth nothing - it is the '^M' character that is boofing you up.
    Open testing2 in vi to it plainly.
    You can use sed to convert the ctrlM's to /r or /n or whatever you want.
    also worth noting - just try to even cat testing2, you'll see grep isn't actually the problem.

  4. #4
    Join Date
    Jun 2008
    Beans
    30

    Wink Re: grep returns odd results based on EOL

    Thanks for those suggestions. I realized that it had to do with the line endings, but typically grep will return the file name FIRST. In my results it doesn't return the filename (which is actually kind of the point of this exercise for me).

    Would you consider this a feature or a bug? Also, can you explain the different results when printing to screen versus printing to file?
    Last edited by thatsmyboy; April 11th, 2012 at 02:38 PM.

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,243
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: grep returns odd results based on EOL

    grep returns matches with filenames when there are multiple files given (with * or whatever), when there is only one file or when it gets data from stdandard input via pipe, there is no name in the output.

    Would you consider this a feature or a bug?
    what, not working with newlines in foreign formats? A fact of life. Normalize everything to the format native to the platform you work on (linux in this case) and everything will be peachy (with sed 's/\r/\n/g' or whatever). Whole toolchain assumes \n, go with it to save yourself a lot of pain.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •