This example shows how to add a character to the beginning of each line using a sed command and bash shell.
Let's create example file.txt with some text:

add 
character
at the
beginning of
each line

Add character at the beginning of each line using sed command. For example to add # in front of each line we can use sed command with following syntax:

$ sed 's/^/#/' file.txt
#add
#character
#at the
#beginning of
#each line

replace # with ' ' ( space ) to add space in front of each line:

$ sed 's/^/ /' file.txt
 add
 character
 at the
 beginning of
 each line

Redirect the output produced by sed command to save it to a file:

$ sed 's/^/ /' file.txt  > new-file.txt
$ cat new-file.txt
 add
 character
 at the
 beginning of
 each line

Free Linux eBooks

Do you have the right skills?

Our IT Skills Watch page reflects an up to date IT skills demand leaning towards the Linux and Unix environment. We have considered a number of skills and operating systems.

See the result...

Linux Online Training

Learn to run Linux servers and prepare for LPI certification with Linux Academy. 104 available video lessons with PDF course notes with your own server!

Go to top