1

I want to specify the output from text file in one command as below:

  1. Print all lines that have all consecutive duplicate characters.
  2. Print all lines with all consecutively repeated characters except the last one or the last two characters in the same line.
  3. Print all lines with all consecutively repeated characters except the first one or the first two characters in the same line.

Example:

11122323 1112266 44778 223334456 6778811 845511 3357788

The output should be

1112266   >>>>> All repeated characters. 
44778     >>>>> All repeated except the last character.
223334456   >>> All repeated except the last two characters
6778811    >>>> All repeated except the first character.
845511     >>>> All repeated except the first two characters.

Non consecutively repeating characters are allowed, but only if 1st or 2nd from start or end of a line. First line excluded because it has 3 not consecutively repeated.

I have tried the following command, but it also finds non consecutive duplicates characters.

awk '
        {split ("", N)                                                  # delete N array
         P = 1                                                          # reset boolean L used for print decision
         L = length
         for (i=1; i<=L; i++) N[substr($0, i, 1)]+=((i<3)||(i>L-2))?2:1 # calculate  char count; doubly weigh leading/trailing
         for (n in N)   if (N[n] < 2)   {P = 0                          # for non-duplicate chars: set print decision
                                         break                          # and quit the for loop
                                        }

        }
P                                                                       # print if non-duplicate chars exist only at margins
' file
  • I have this sed command but it works only with the lines that all with constructively repeated characters, how to exclude the last one or the last two characters from being duplicated? ""sed -Ee '/^((.)\2+)+$/!d' input.txt"" – Ahmed Oct 22 '18 at 19:54
  • Hello muru could you please edit the above commands to work with me as required i have tried . – Ahmed Oct 22 '18 at 20:06
  • perl -lne 'print if /^((.)\2+(?!\2))+$/' This command i have tried also but it only for lines with all consecutive duplicates.How to exclude first or second characters ot together from the end from being duplicated. Any edition to this command Thanks. – Ahmed Oct 22 '18 at 21:02
  • Why do all 3 requirements have to be met by "one command"? – Jeff Schaller Oct 23 '18 at 0:19
  • @ Jeff Schaller ok separately. – Ahmed Oct 23 '18 at 6:50

Your Answer

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

Browse other questions tagged or ask your own question.