When you mention 'at positions 4,8 and 22', sed means 'one after positions 3,7 and 21'. You want to insert and keep the existing text as is.
This?:
sed -i -r "s/^(.{3})/\1,/;s/^(.{7})/\1,/;s/^(.{21})/\1,/" $(dirname "$0")/blank.txt
-i = --in-place aka change directly in the existing file, -r = regular expression, \1 = refers to the 'group' between parenthesis, $(dirname
"$0") = to stay in the current dir
A similar sed-use when you want to e.g. set tabs instead of spaces at certain positions in a file with lines seperated by single spaces:
sed -i -r "s/^(.{10})./\1\t/;s/^(.{20})./\1\t/;s/^(.{30})./\1\t/" $(dirname "$0")/blank.txt
Substitutes the characters (in this case single spaces) at position 11, 21 and 31 by a tab. The spaces within the created columns stay untouched.
You can still get the impression that this doesn't align/display your columns well in your text-editor. The coded tabs aren't wrong, but often you need to adapt the tab-setting in the text-editor preferences. E.g. Geany -> Preferences -> Editor -> Indentation -> Tabs width 8 instead of 4.
(The latter somewhat aside your original question...)
sed s/^...../&,/ /path/to/file
will do it.s/^\(.\{4\}\)/\1,/
tos/^.\{4\}/&,/
and still be usable as anex
command or withinvi
. (Of course it could be further simplified tos/./&,/4
for use insed
.)