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

Go to top