![특정 컨텍스트에서 문자 제거(셸 스크립트 사용)](https://linux55.com/image/160865/%ED%8A%B9%EC%A0%95%20%EC%BB%A8%ED%85%8D%EC%8A%A4%ED%8A%B8%EC%97%90%EC%84%9C%20%EB%AC%B8%EC%9E%90%20%EC%A0%9C%EA%B1%B0(%EC%85%B8%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%20%EC%82%AC%EC%9A%A9).png)
그래서 다음과 같은 이름 목록이 포함된 파일이 있습니다.
Thomas Newbury
Calvin Lewis
E. J. Frederickson
Lamar Wojcik
J.C. Lily
Lillian Thomas
나는 결국 그것들을 이름과 성의 긴 목록으로 나누려고 노력할 것입니다. 그러나 그 전에 "EJ"를 "EJ"로 바꾸고 싶지만 bash로 어떻게 해야 할지 모르겠습니다.
"EJ"와 일치하는 것은 알고 있지만 "[A-Z]+. [A-Z]+."
두 점 문자 사이의 문맥에서만 공백을 제거할 수 있는 명령이 무엇인지 모르겠습니다.
답변1
나는 이것이 GNU에서 작동한다고 생각합니다 sed
.
sed -E 's/^([A-Z]+\.)[[:blank:]]([A-Z]+\.)/\1\2/' file
답변2
내 생각에는 sed가 최선의 선택이라고 생각합니다. 제 버전은 다음과 같습니다.
sed -r ':a;s/^(.*\.)(\ )+(.\.)(.*)$/\1\3\4/;t a' file
-r -- use extended regular expressions
:a -- label "a"
^(.*\\.) -- 1st group matches any character "." from the line beginning up to a literal "\\.".
(\ )+ -- 2nd group matches white space (+ is one or more)
(.\.) -- 3rd group matches the next letter
(.*)$ -- 4th group matches to the end of the line
;t a -- if the previous substitution did something then branch to label "a"
/\1\2\4/ -- replaces the matches with groups 1,3,4 removing the space
이는 임의의 약어를 처리할 수 있습니다(예: SOV Sovereign).