If you want to stick to grep
, use a scripting language such as Perl to generate the regex programmatically. For example:
perl -le 'print join "", map "[${_}N]", split //, $ARGV[0];' ATCGCTATCG
Prints:
[AN][TN][CN][GN][CN][TN][AN][TN][CN][GN]
You can use it in grep
like so:
grep '[AN][TN][CN][GN][CN][TN][AN][TN][CN][GN]' <<< ATNGCNATCG
Prints:
ATNGCNATCG
If that works for you, you could make it into a little bash function that also runs the grep
. Add these lines to your ~/.bashrc
:
grepN(){
seq="$1"
file="$2"
pattern=$(perl -le 'print join "", map "[${_}N]", split //, $ARGV[0];' "$seq")
grep "$pattern" "$file"
}
You can now run
grepN ATCGCTATCG my.fastq
Of course, this is not a good idea since the sequence might be in different lines, but that's what you were doing originally.