I have below commands for extracting only the lines that will all consecutive duplicates two or more times I want to edit these sed and perl commands to exclude the first and the second character from the end of a line from being duplicated.

perl -lne 'print if /^((.)\2+(?!\2))+$/' 
sed -Ee '/^((.)\2+)+$/!d' input.txt

I want to print only the lines with all consecutively repeated characters except the last one or the last two characters in the same line. Non consecutively repeating characters are allowed, but only if 1st or 2nd from the end of a line of my big text file.

For example input file contains:


The output file should contain:

111224    (only last character is not consecutively repeated)
447758    (only the last two characters are not consecutively repeated)


  • Hello roaima I mean that non consecutive duplicates are allowed only at the first or the second character from the end.
    – Ahmed
    Oct 23, 2018 at 17:00
  • Which sentence is it ok now?
    – Ahmed
    Oct 23, 2018 at 17:11
  • Your first sentence. I think it's missing a couple of words and a full stop somewhere - it feels like two half sentences crashed together. Oct 23, 2018 at 17:33

1 Answer 1


Based on my answer from a previous question:

sed -En 'h;:a;s/^(.)\1+//;ta;/^.{1,2}$/{x;p}' file
  • @ glenn jackman thank you for your answer. But how to exclude the first or the second character from the Start of the line from being consecutive duplicated?
    – Ahmed
    Oct 23, 2018 at 17:43
  • 1
    You are solidly in the "do my job for me" territory: Please see: Why is “Can someone help me?” not an actual question? Oct 23, 2018 at 17:58
  • Dear glenn jackman there is a difference between who want to depend on others and who want to benefit from their experiences.
    – Ahmed
    Oct 23, 2018 at 18:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .