How to insert text string at the beginning of the line
Using sed command we can insert the text at any matching pattern of the file. Using sed we can insert at any point of the file.
Insertion with sed at the beginning of the line
If you want to insert the text during the execution of the command use -i option.
Sed -i 's/^/begin here/' my_input_filename
In the above example ^ represents the beginning of the line and will add “begin here” at the beginning of every line of input file.
If you want to redirect the output to another file use the redirect option.
sed 's/^/begin here/' my_input_filename > newfilename
This will insert “begin here” at the beginning of every line and redirect its output to a new specified file.
Insert at the beginning of the line containing a pattern
sed -i '/pattern/ s/^/ text to add/' my_input_filename
The first part is a pattern to find and the second part is an ordinary sed’s substitution using ^ for the starting of a line.