I would like to count the number of strings in a document.
If input is:
GGTGGTGGTAT
GGTAGTGGTAT
GGTGGTGGTAT
GGTAATGGTAT
And I search for GGTGGTGGT I would like to find 3 matches. Allowing for one ambiguity.
Using egrep it would look something like this and have an output of 3.
egrep -c "GGTGGTGGT|.GTGGTGGT|G.TGGTGGT|GG.GGTGGT|GGT.GTGGT|GGTG.TGGT|GGTGG.GGT|GGTGGT.GT|GGTGGTG.T|GGTGGTGG." input
Here's a way to generate that regex with bash:
$ patt=(GGTGGTGGT)
$ for ((i=0; i<${#patt[0]}; i++)); do
patt+=( "${patt[0]:0:i}.${patt[0]:i+1}" )
done
$ regex=$(IFS='|'; echo "${patt[*]}")
$ echo "$regex"
GGTGGTGGT|.GTGGTGGT|G.TGGTGGT|GG.GGTGGT|GGT.GTGGT|GGTG.TGGT|GGTGG.GGT|GGTGGT.GT|GGTGGTG.T|GGTGGTGG.
and then:
awk -v regex="$regex" '$0 ~ regex' file
Or with awk only:
awk -v srch=GGTGGTGGT '
BEGIN {
regex = srch
for (i=1; i<=length(srch); i++)
regex = regex "|" substr(srch,1,i-1) "." substr(srch, i+1)
}
$0 ~ regex
' << END
GGTGGTGGTAT
GGTAGTGGTAT
GGTGGTGGTAT
GGTAATGGTAT
END
GGTGGTGGTAT
GGTAGTGGTAT
GGTGGTGGTAT
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With