regexgrep

grep regex for lines with digit pairs (or more)


I've been working out how to formulate the proper regex, where the file format is such:

2999 1245 8765 2323
4545 1213 1332 6543
1234 4567 6789 0124
1234 5678 9012 3456

Matching only lines that have consecutive matching integers. Spaces between do not matter.

The above should return:

2999 1245 8765 2323
# [9][9][9] meets the requirement 
4545 1213 1332 6543
# [3][3] meets the requirement 
1234 4567 6789 0124
# [4][4] over the space separated still holds

The last line does not match.

So far I have been trying:

$ grep -E '([0-9]+) \1' input-file.txt

Where the \1 is the group but I don't match all of the possible groups.


Solution

  • I suggest:

    grep -E '([0-9]) ?\1+' file
    

    Output:

    2999 1245 8765 2323

    4545 1213 1332 6543

    1234 4567 6789 0124


    Quantifiers check to see how many times to search for a character:

    ?: zero or one

    +: one or more

    *: zero or more