add space 5 times at the beginning of each line in a text file
With GNU sed:
sed -i -e 's/^/ /' <file>
will replace the start of each line with 5 spaces. The -i
modifies the file in place, -e
gives some code for sed
to execute. s
tells sed
to do a subsitution, ^
matches the start of the line, then the part between the second two /
characters is what will replace the part matched in the beginning, i.e., the start of the line in this example.
You can use sed
sed 's_^_ _' tmpin > tmpout
Or awk
awk '{print " " $0}' tmpin > tmpout
Or paste (Thanks cuonglm)
:| paste -d' ' - - - - - file
Watch out. These can be addictive. You can solve many simple problems, but the time will come where you need to upgrade to a full scripting language.
Edit: Sed script simplified based on Eric Renouf's answer.
You can use many standard tools, example with paste
:
:| paste -d' ' - - - - - file
or shorter with awk
:
awk '{$1=" "$1}1' file
or more portable with perl
:
perl -pe '$_=" "x5 .$_' file