Does anyone know of a UNIX command or series of commands to list lines that contains 3 matching numbers to a search with 6 numbers? For example, searching with 38 39 40 41 42 43 I would like to see combinations like 38 40 43 or 39 41 42 listed per line. Thanks.

  • Do you want only lines where these numbers are separated only by a space, and do these numbers need to be distinct and/or in the order you typed them? So what about 38 38 40 or 38 43 40? – Stefan Hamcke Dec 29 '18 at 21:42
  • Unix or UNIX. Unix is the generic name for e.g. BSD, System-V, Gnu, etc. UNIX is a brand name. – ctrl-alt-delor Dec 29 '18 at 23:27

Assuming that you have a file named file that contains lines with mutually different whitespace-separated numbers, and you have a set of 6 numbers - e.g. 38, 39, 40, 41, 42, 43 - that you want to search for in combinations of three, such that lines that consecutively contain three of these numbers in random order are listed as a result of the search, you could use:

grep -P  '((^|\s+)(38|39|40|41|42|43)(?=($|\s))){3}' file

which uses grep with a Perl regular expression (PCRE).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.