Replace all first 4 spaces with a tab

I am doing some work with documentation, and I have such a tree structure:

A
    BB
    C   C
        DD    

How can I replace only all occurrences of two spaces in a line string with '-', for example:

A
--BB
--C   C
----DD

I tried sed 's/ /-/g', but this replaces all occurrences of 2 spaces; also sed 's/^ /-/g', it simply replaces the first occurrence of 2 spaces. How can i do this?

+4
source share
1 answer

A regular expression for four spaces at the beginning of a line /^ /, where I put slashes just to delimit the expression (they are not part of the real regular expression, but they are used as delimiters on sed).

sed 's/^    /\t/' file

sed -i file (.. sed ); * BSD ( OSX), -i '' .

escape- \t ; , , , , ctrl-V.

( "", . , \t script --, .)

" , ", sed, Perl:

perl -pe 's%^((?:  )+)% "-" x (length($1) / 2)%e' file

$1; , + , . /e Perl- ; "-" , , .

+6

All Articles