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
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed on LinuxCareers.com.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in:



Comments and Discussions