
텍스트 파일이 있고 다음과 같은 명령으로 출력을 분리하고 싶습니다.
- 연속된 반복 문자가 모두 포함된 모든 줄을 인쇄합니다.
- 같은 줄의 마지막 또는 마지막 두 문자를 제외하고 연속적으로 반복되는 모든 문자를 포함하는 모든 줄을 인쇄합니다.
- 같은 줄의 처음 또는 처음 두 문자를 제외하고 연속적으로 반복되는 모든 문자를 포함하는 모든 줄을 인쇄합니다.
예: 11122323 1112266 44778 223334456 6778811 845511 3357788
출력은
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.
반복되지 않는 문자는 줄의 시작이나 끝에서 첫 번째 또는 두 번째 문자인 한 허용됩니다. 첫 번째 행은 #3이 연속적으로 반복되지 않으므로 제외됩니다.
답변1
조금 익숙해지세요최근 답변비슷한 질문이 있는 경우:
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