Yanıtlar:
Gönderdiğiniz komut dosyasında 4 * n boşluklar yalnızca sekmelerden önce geliyorsa n sekmeye dönüştürülür.
4 boşluğu 2 boşlukla değiştirmek istiyorsanız, ancak sadece girinti ile, sed ile yapmak mümkün olsa da, bunun yerine Perl'i öneriyorum.
perl -pe 's{^((?: {4})*)}{" " x (2*length($1)/4)}e' file
Sed'de:
sed -e 's/^/~/' -e ': r' -e 's/^\( *\)~ /\1 ~/' -e 't r' -e 's/~//' file
Bunun indentyerine kullanmak isteyebilirsiniz .
Basit bir şekilde çalışmaz:
sed -r 's/ {4}/ /g'
Değilse, bazı girişleri başarısız olduğu yere gönderin.
Yalnızca önde gelen boşluklar dönüştürülecekse:
sed 'h;s/[^ ].*//;s/ / /g;G;s/\n *//'
Yorumlarla:
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'
Ayrıca vim'in 'ts'ayarına ve :retabkomutuna bakın
'ts've :retabsorunun çözümü değildir, ancak birbiriyle ilişkilidir ve genel hedefinize ulaşmanıza yardımcı olabilir. Yapabilirsin vim -- *.c, :set ts=...ve sonra :argdo retabya :argdo retab!. Ayrıca bkz. 'sw'Seçenek ve vim'in kendi girintileme özellikleri.
sed 's/^\( \+\)\1\1\1/\1\1/' file
Önde gelen alanları aynı grubun dört örneğine bölerek çalışır (böylece hepsi eşittir) ve ardından bunları grubun yalnızca iki örneğiyle değiştirir.
\+). Teşekkür ederim.
Nested quantifiers in regex; marked by <-- HERE in m/^( {4}* <-- HERE )/ at -e line 1.