You can use grep
if you are using the GNU version of grep
, which has the -P
option for perl-compatible regular expressions (PCRE).
Alternatively, you could use pcregrep
(aka pgrep
) by the author of the PCRE library. There's not much reason to use this these days unless you are using an old version of GNU grep, or a non-GNU grep, that doesn't suppoer the -P
option and can't be upgraded or replaced.
e.g. (assuming that the sequences are in a file called input.txt
):
$ grep -oP 'ATG.*?TA[AG]' input.txt
ATGCTAG
ATGATCAATGCCCGTATAG
The -o
option tells GNU grep to output only the matching text, not the entire line, and the -P
tells it to use perl-compatible regular expressions.
Or, if you're using pcregrep
:
$ pcregrep -o 'ATG.*?TA[AG]' input.txt
ATGCTAG
ATGATCAATGCCCGTATAG
The non-greedy ?
modifier in the regexp (.*?
) ensures that it captures ALL matching patterns, not just the longest one. In the context of regular expressions, "greedy" means "try to match as MUCH as possible" (the default), and "non-greedy" means "try to match as LITTLE as possible".
There's a good explanation of this at https://www.regular-expressions.info/repeat.html. BTW, the rest of that site is a pretty good place to learn about regular expressions, with lots of tutorials and examples.
Note that most regular expression libraries do not implement non-greedy matches, it is a perl extension that has also been adopted by GNU grep. and programs linked with PCRE, of course.
BTW, This is what the output would look like without the non-greedy modifier:
$ grep -oP 'ATG.*TA[AG]' input.txt
ATGCTAGCCCGTATAGCGATACTAGCTATACGATATATATGATCAATGCCCGTATAG
sed
,awk
andgrep
have nothing to do with bash. Awk itself is a full programming language, so allowing you to useawk
and notperl
is absurd. They are the same class of thing. Evensed
is Turing-complete! This sounds like you need to ask your professor to explain what they need. No sane bioinformatician would ever do this in pure bash. But, if you must, I have posted one approach below. It only deals with the forward strand, you can adapt it to deal with the reverse. – terdon♦ Oct 14 '19 at 9:55