어떻게 4 개의 공백을 2 개의 공백으로 변환 sed합니까? 가능합니까?
이것을 찾았지만 탭을 공백으로 변환합니다.
sed -r ':f; s|^(\t*)\s{4}|\1\t|g; t f' file
답변
게시 한 스크립트는 4 * n 공백을 탭이 앞에있는 경우에만 4 개의 n 공백을 n 개의 탭으로 변환합니다.
4 개의 공백을 2 개의 공백으로 바꾸고 만 들여 쓰기 만하고 sed로 할 수는 있지만 대신 Perl을 권장합니다.
perl -pe 's{^((?: {4})*)}{" " x (2*length($1)/4)}e' file
sed에서 :
sed -e 's/^/~/' -e ': r' -e 's/^\( *\)~    /\1  ~/' -e 't r' -e 's/~//' file
indent대신 사용할 수 있습니다 .
답변
간단한 방법으로 작동하지 않습니다.
sed -r 's/ {4}/  /g'
그렇지 않은 경우 실패한 곳에 입력을 게시하십시오.
답변
선행 공백 만 변환해야하는 경우 :
sed 'h;s/[^ ].*//;s/    /  /g;G;s/\n *//'
의견 :
sed '
  h; # save a copy of the pattern space (filled with the current line)
     # onto the hold space
  s/[^ ].*//; # remove everything starting with the first non-space
              # from the pattern space. That leaves the leading space
              # characters
  s/    /  /g; # substitute every sequence of 4 spaces with 2.
  G; # append a newline and the hold space (the saved original line) to
     # the pattern space.
  s/\n *//; # remove that newline and the indentation of the original
            # line that follows it'
또한 vim의 'ts'설정과 :retab명령을보십시오
답변
sed 's/^\( \+\)\1\1\1/\1\1/' file
선행 공백을 동일한 그룹의 4 개의 인스턴스로 나누고 (모두 동일) 그룹의 두 인스턴스로만 교체합니다.
답변
sed 's/    \{2,4\}\( \{0,1\}[^ ].*\)*/  \1/g' <input
그것은 일련의 공백을 쥐어 짜야합니다.
답변
