Grep not working on mac

Hi all,
I got a new mac and can't get grep, awk etc to work.

I tried the following command:

grep DICER test.txt

output:

AGOER

text.txt looks like this:

DICER
DICER
AGO

What is wrong?

Please show the output of

echo $PATH
robinm$ echo $PATH

/usr/local/Cellar/coreutils/8.23_1/libexec/gnubin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

I installed the Homebrew package

Hello,
What is file format (unix,dos,mac) ?
I think file format is mac but your tools work with unix format...
examples:

$ grep DICER MAC.txt
DICER
DICER
$ unix2mac MAC.txt
unix2mac: conversion du fichier MAC.txt au format Mac...
$ grep DICER MAC.txt
AGOER

I saved it as tab delimited text (.txt) from excel on mac.

I also tried another command that did not work:

sed '/^\s*$/d' Endonuclease_GO-0004519_Filter.txt

Output:

E3C5X5

DCL4
DICER
CAS1A
CAS2A
APE

T2BA
ENDOA

The empty lines remains, even if the function is supposed to remove them.
What is wrong with my files??

Post the output of od -ctx1 run on your files.

Endonuclease_GO-0004519_Filter.txt | head
0000000   E   3   C   5   X   5  \r  \n  \r  \n   D   C   L   4  \r  \n
         45  33  43  35  58  35  0d  0a  0d  0a  44  43  4c  34  0d  0a
0000020   D   I   C   E   R  \r  \n   C   A   S   1   A  \r  \n   C   A
         44  49  43  45  52  0d  0a  43  41  53  31  41  0d  0a  43  41
0000040   S   2   A  \r  \n   A   P   E  \r  \n  \r  \n   T   2   B   A
         53  32  41  0d  0a  41  50  45  0d  0a  0d  0a  54  32  42  41
0000060  \r  \n   E   N   D   O   A  \r  \n   N   F   I  \r  \n   R   E
         0d  0a  45  4e  44  4f  41  0d  0a  4e  46  49  0d  0a  52  45
0000100   C   U  \r  \n   Y   O   K   F  \r  \n   Q   9   M   C   N   1
         43  55  0d  0a  59  4f  4b  46  0d  0a  51  39  4d  43  4e  31

PLease use code tags as required by forum rules!

There you are - it's neither *nix nor MAC, it's DOS: 0d 0a as line terminator. Reformat with dos2unix (I don't know about dos2mac) and try again.
Or, try /^\s*\r$/d for your sed command.

1 Like

Thanks!

What is the best way to save excel files to make them work in the terminal?

csv (comma separated values/variables) or <TAB> delimited files is fine; just make sure the line terminator is selected appropriately.

The line terminator shouldn't matter here -- DICER\r matches the regex DICER .

The carriage returns may be messing up your console, though. Try grep -l. It will print the filename instead of the file, if it happens to match.

grep -l DICER filename

Sorry for bothering again.
I have my file:

Endonuclease_GO-0004519_Filter.csv

E3C5X5

DCL4
DICER
CAS1A
CAS2A
APE

T2BA
ENDOA

I saved it as windows comma separated csv:

Endonuclease_GO-0004519_Filter.csv |head

0000000   E   3   C   5   X   5  \r  \n  \r  \n   D   C   L   4  \r  \n
         45  33  43  35  58  35  0d  0a  0d  0a  44  43  4c  34  0d  0a
0000020   D   I   C   E   R  \r  \n   C   A   S   1   A  \r  \n   C   A
         44  49  43  45  52  0d  0a  43  41  53  31  41  0d  0a  43  41
0000040   S   2   A  \r  \n   A   P   E  \r  \n  \r  \n   T   2   B   A
         53  32  41  0d  0a  41  50  45  0d  0a  0d  0a  54  32  42  41
0000060  \r  \n   E   N   D   O   A  \r  \n   N   F   I  \r  \n   R   E
         0d  0a  45  4e  44  4f  41  0d  0a  4e  46  49  0d  0a  52  45
0000100   C   U  \r  \n   Y   O   K   F  \r  \n   Q   9   M   C   N   1
         43  55  0d  0a  59  4f  4b  46  0d  0a  51  39  4d  43  4e  31

When I try to remove blank lines, it does not do it:

sed /^\s*\r$/d   Endonuclease_GO-0004519_Filter.csv |head

E3C5X5

DCL4
DICER
CAS1A
CAS2A
APE

T2BA
ENDOA

You must quote sed command either \s \r * are interpreted by shell:

sed '/^\s*\r$/d'   Endonuclease_GO-0004519_Filter.csv |head

Regards.

It still does not work:

sed '/^\s*\r$/d' Endonuclease_GO-0004519_Filter.csv |head
E3C5X5

DCL4
DICER
CAS1A
CAS2A
APE

T2BA
ENDOA

works for me when single quoted:

sed '/^\s*\r$/d' file
E3C5X5
DCL4
DICER
CAS1A
CAS2A
APE
T2BA
ENDOA

Alternate methods: use the <CTRL>-V lead-in for <CR>: sed /^\s*^M$/d file or the shell construct $'\r' : sed /^\s*$'\r'$/d file .

Gotta be something with my mac setup..

Ok, could you try:

sed '/^\r$/d' Endonuclease_GO-0004519_Filter.csv |head

Regards.

Still don't work

sed '/^\r$/d' Endonuclease_GO-0004519_Filter.csv |head
E3C5X5

DCL4
DICER
CAS1A
CAS2A
APE

T2BA
ENDOA

Could you post ouput of 2 commands:

od -ctx1 Endonuclease_GO-0004519_Filter.csv | head

and

sed '/^\r$/d' Endonuclease_GO-0004519_Filter.csv | od -ctx1  | head

Regards.

sed '/^\r$/d' Endonuclease_GO-0004519_Filter.csv | head
E3C5X5

DCL4
DICER
CAS1A
CAS2A
APE

T2BA
ENDOA
sed '/^\r$/d' Endonuclease_GO-0004519_Filter.csv | od -ctx1  | head
0000000   E   3   C   5   X   5  \r  \n  \r  \n   D   C   L   4  \r  \n
         45  33  43  35  58  35  0d  0a  0d  0a  44  43  4c  34  0d  0a
0000020   D   I   C   E   R  \r  \n   C   A   S   1   A  \r  \n   C   A
         44  49  43  45  52  0d  0a  43  41  53  31  41  0d  0a  43  41
0000040   S   2   A  \r  \n   A   P   E  \r  \n  \r  \n   T   2   B   A
         53  32  41  0d  0a  41  50  45  0d  0a  0d  0a  54  32  42  41
0000060  \r  \n   E   N   D   O   A  \r  \n   N   F   I  \r  \n   R   E
         0d  0a  45  4e  44  4f  41  0d  0a  4e  46  49  0d  0a  52  45
0000100   C   U  \r  \n   Y   O   K   F  \r  \n   Q   9   M   C   N   1
         43  55  0d  0a  59  4f  4b  46  0d  0a  51  39  4d  43  4e  31