So I have this problem where I need to use the grep command. So I got 52 cards of 13 ranks and 4 colors.
The ranks consist of A,2,3,4,5,6,7,8,9,T(for ten),J,Q and K(king). The four colors: c,d,h,s.
Now I've got file (cards.txt) with all possible combinations of 13 cards. Example: 8cKc6s4dKd8sQc4c2s6h9dTc4h
Now the question is output all combinations that contains 4 cards of the same ranks.
β = { h ∈ H | h contains 4 cards of the same ranks}. examples:
Kd9dJs5sKs7c5c6cKcJhKhTh7h ∈ β
AdTdTc2d2cTsKh6c3c6s6dKc4h ∉ β
(Problem is I know how to use grep command for sequence of characters but only when they're next to each other. Help plz)
I guess you want back-references (see this link):
grep '\([A23456789TJQK]\).*\1.*\1.*\1' cards.txt
If grep matches a character from A23456789TJQK, then, because of the parentheses \(...\), grep will refer to it as \1 (this is the back-reference).
The pattern can also be written as follows:
grep '\([A23456789TJQK]\)\(.*\1\)\{3\}' cards.txt
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