1

I want to mark the same digits, which can repeat 3 times. My command:

grep -E '[[:digit:]]{2,3}' file

mark every digits, but I want to one the same digit.

Input:

11111 222 33
2223345678912345678901234
5

Output:

11111 222 33
2223345678912345678901234
5

I want to:

11111 222 33<-mark 222
2223345678912345678901234<-mark 222
5
  • Please add sample input and your desired output for that sample input to your question. – Cyrus Aug 30 '15 at 11:41
  • I edited my post. – diego9403 Aug 30 '15 at 11:45
  • Where is the difference between input and output? – Cyrus Aug 30 '15 at 11:47
  • There is no difference, because of my grep command. – diego9403 Aug 30 '15 at 11:52
  • Replcae output by your desired output. – Cyrus Aug 30 '15 at 13:32
0

I have found that you can grep a file for 3 consecutive numbers using the following command:

grep "\([0-9]\)\\1\\1" chatlog.txt

This marks all lines that have 3 consecutive identical numbers. the \\1 matches the character that was matched in the first group, in this case a number.

So to match 4 identical numbers you would want:

grep "\([0-9]\)\\1\\1\\1" chatlog.txt

You can define this in a more flexible way:

grep "\([0-9]\)\(\\1\)\{2,3\}"

The above statement will match series of 3 or 4 consecutive identical numbers. Mind that the first group matches the first digit. Afterwards you match that group either two or three more times.

So given the input:

one: 1
two: 11
three: 111
four: 1111

The following command:

grep -r "\([0-9]\)\(\\1\)\{2,3\}" test.txt

Returns:

three: 111
four: 1111
2
  • Ok that's very good. But I want to make a interval, I mean that digits can be in the line one, two or three times in the line. – diego9403 Aug 30 '15 at 14:03
  • Does the above edit answer your question? – Christophe De Troyer Aug 30 '15 at 14:44

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.