回答:
投稿したスクリプトは、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コマンドもご覧ください
'ts'と:retabは質問に対する解決策ではありませんが、関連しており、全体的な目標の達成に役立つ場合があることに注意してください。あなたができるvim -- *.c、:set ts=...そして:argdo retabまたは:argdo retab!。'sw'オプションとvimの独自のインデント機能も参照してください。
sed 's/^\( \+\)\1\1\1/\1\1/' file
これは、先頭のスペースを同じグループの4つのインスタンスに分割し(それらがすべて等しい)、それらをグループの2つのインスタンスのみに置き換えることで機能します。
\+)。ありがとうございました。
Nested quantifiers in regex; marked by <-- HERE in m/^( {4}* <-- HERE )/ at -e line 1.