sed: 전역적으로 교체할 때 선행 공백을 무시합니다.

sed: 전역적으로 교체할 때 선행 공백을 무시합니다.

파일의 과도한 공백을 대체하기 위해 sed 명령을 작성하려고 합니다. 각 단어 사이에는 하나의 공백만 있을 수 있지만 선행 공백과 탭은 그대로 두어야 합니다. 따라서 파일은 다음과 같습니다.

     This is     an indented      paragraph. The   indentation   should not be changed.
This is the     second   line  of the    paragraph. 

될 것입니다:

     This is an indented paragraph. The indentation should not be changed.
This is the second line of the paragraph.

내가 시도한 변형

/^[ \t]*/!s/[ \t]+/ /g

어떤 아이디어라도 크게 감사하겠습니다.

답변1

$ sed 's/\>[[:blank:]]\{1,\}/ /g' file
     This is an indented paragraph. The indentation should not be changed.
This is the second line of the paragraph.

내가 사용하는 표현식은 하나 이상 [[:blank:]](스페이스 또는 탭) 과 일치합니다.한 문장 뒤에, 공백으로 바꿉니다. \>단어 문자와 단어가 아닌 문자 사이의 너비가 0인 경계 와 일치합니다 .

이것은 OpenBSD에서 기본적으로 테스트되었지만 sedGNU에서도 작동해야 한다고 생각합니다. sedGNU는 단어 경계를 일치시키는 sed데에도 사용됩니다 .\b

sed -E다음과 같이 단축 할 수도 있습니다 .

sed -E 's/\>[[:blank:]]+/ /g' file

다시 말하지만, \>GNU가 작동하지 않으면 sed대신 사용하십시오 \b.


위의 내용은 예제 텍스트를 올바른 방식으로 정렬하지만 그렇지 않습니다.상당히첫 번째 문장 뒤와 같이 구두점 뒤의 공백을 제거하는 데 사용됩니다.

     This is     an indented      paragraph.        The   indentation   should not be changed.
This is the     second   line  of the    paragraph.

이를 위해 약간 더 복잡한 변형이 트릭을 수행합니다.

$ sed -E 's/([^[:blank:]])[[:blank:]]+/\1 /g' file
     This is an indented paragraph. The indentation should not be changed.
This is the second line of the paragraph.

이는 공백이 아닌 문자 뒤에 하나 이상의 공백 문자가 오는 것을 공백이 아닌 문자와 공백으로 대체합니다.

또는 표준을 사용하십시오 sed(존재하는 경우에만 대체되므로 매우 작은 최적화).둘 이상공백/탭이 아닌 부분 뒤에 공백/탭이 있음),

$ sed 's/\([^[:blank:]]\)[[:blank:]]\{2,\}/\1 /g' file
     This is an indented paragraph. The indentation should not be changed.
This is the second line of the paragraph.

답변2

POSIX적으로:

sed 's/\([^[:space:]]\)[[:space:]]\{1,\}/\1 /g; s/[[:space:]]*$//'

공백이 아닌 공백 뒤에 오는 하나 이상의 공백 문자 시퀀스를 해당 공백이 아닌 SPC 문자로 바꾸고, 후행 공백 문자를 제거합니다. 그러면 빈 줄과 줄을 후행 공백으로 덮어씁니다(Microsoft 끝의 텍스트 파일 포함). 선).

관련 정보