This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Why doesn't my grep expression work?

Hello,

I am trying to execute a very simple grep command, but I can't get it to work and I cannot figure out why. Here is exactly what I am doing:

echo 'ACTTTATTA' > myseq

cat myseq

ACTTTATTA

grep -E '^.{2}TTT' myseq

No results

I am trying to grep a sequence that has 3 Ts starting from the third nucleotide, just like the one I am creating above. But I get no output. The error seems to come from {2}, because I have been able to reproduce this behaviour only when I introduce {number} and it gets solved when I replace it by something else.

I have:

  • Tested it in a different computer

  • Tested it in google colab

  • Tested it in regex101 (it works as expected)

What am I doing wrong?

grep regex

What's the exclamation mark before the grep?

This worked for me:

echo 'ACTTTATTA' | grep -E '^.{2}TTT'

ACTTTATTA

This should work too

cat myseq | grep -E '^.{2}TTT'

Thank you! The "!" sign is used in google colab to execute a terminal command. I included it by mistake and have corrected it.

It looks like your command works in my computer but does not work in google colab. Can this be due to some different version of grep? Do you think you could check that your code indeed does not work in your hands in google colab jut to make sure I am not messing up?

I don't use googlecolab, but maybe you can use awk or try using " instead of ' and see if it works there:

 echo "ACTTTATTA" | grep -oP "^.{2}TTT.*"

echo "ACTTTATTA" | grep -E "^.{2}TTT"

The following worked for me:

% grep -E '^.{2}TTT' <(echo -e 'ACTTTATTA')
ACTTTATTA
% grep -E '^.{2}TTT' <(echo -e 'AACTTTATTA')
% grep -E '^.{2}TTT' <(echo -e 'CTTTATTA') 
% grep -E '^.{2}TTT' <(echo -e 'XCTTTATTA')
XCTTTATTA
% grep -E '^.{2}TTT' <(echo -e 'XCTTTATTA')

You should also be able to use egrep, as well as the more constrained pattern ^[ACTG]{2}TTT, if you need to:

% egrep '^[ACTG]{2}TTT' <(echo -e 'XCTTTATTA')
% egrep '^[ACTG]{2}TTT' <(echo -e 'ACTTTATTA')
ACTTTATTA

Thank you! As I replied to Fatima above, those seem to work in my computer, but not in google colab, which I need to use for this project. Any chance you have some idea why this might be?

I wonder if python is doing something to the {2}. Could you try:

!echo grep -E '^.{2}TTT' myseq > how_my_command_looks_like

and then check the file?

I think you hit the nail on the head. Something is off:

!cat how_my_command_looks_like

grep -E ^.2TTT myseq

I was able to solve it by first defining a python string and then executing such string:

command = "grep -E '^.{2}TTT' <(echo -e 'XCTTTATTA')"

!$command

XCTTTATTA

Thank you! That was a very good catch. I wonder if there is another solution though...

It could maybe be solved by different quoting or escaping characters, but I never use such things for shell commands so I wouldn't know.

Have you tried this one?

grep -E "^.{2}TTT" <(echo -e "XCTTTATTA")

@OP, is this solved, can one or many of the comments be moved to answer?

0 answers

No answers yet.

Log in to answer this question.