# Print only the lines that have specific properties

LlI have a text file i want to separate the output in one command as below:

1. Print all lines that have all consecutive duplicates 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-repeating characters are allowed, but only if 1st or 2nd from start or end of a line. First line excluded becuse it has #3 is not consecutively repeated.

• This is same question unix.stackexchange.com/questions/472285/… – Zahi Oct 13 '18 at 21:53
• I tried many many times to solve it but no way – Ahmed Oct 13 '18 at 21:55
• perl -lne '(\$copy = \$_) =~ s/(.)\1//g; print if \$copy' INFILE – Ahmed Oct 13 '18 at 21:55
• I have to ask for your help and i will study the answer – Ahmed Oct 13 '18 at 21:57
• Can i know what is the sticking point? – Ahmed Oct 13 '18 at 21:58

Slight adaption to the recent answer to your similar problem:

``````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
1112266
44778
223334456
6778811
845511
``````
• Dear RudiC your answer need some modification because if i have 1111323 in input it will be included in the output but it has to be excluded because it has three non repeated characters from end. For Example (1111323 excluded) but (1111332 included) (11113324 included) in the output. – Ahmed Oct 14 '18 at 13:06
• Dear Ahmed, how about applying what you learned so far and analysing / modifying the proposal yourself? The proposal behaves exactly as specified, and it yields the desired output from your question. `1111323` has `3` duplicated, and `2` in the second last position, so the spec says it has to be printed. – RudiC Oct 14 '18 at 14:38
• could you please just show me how to tell awk that what i mean with duplicates is 1113355 but not 1331515 the repeated characters are one beside one with no seperation. Thanks – Ahmed Oct 14 '18 at 15:42
• could you please just show me how to tell awk that what i mean with duplicates is 1113355 but not 1331515 the repeated characters are one beside one with no seperation. Thanks – Ahmed Oct 14 '18 at 15:42
• Two days ago i'm trying to print the lines that all with only consecutive duplicates characters but coud'nt any help? – Ahmed Oct 15 '18 at 14:02