Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how i can add Add text at the beginning of each line?

how i can add Add text at the beginning of each line?

for example:- i have file contain:-

/var/lib/svn/repos/b1me/products/payone/generic/code/core 
/var/lib/svn/repos/b1me/products/payone/generic/code/fees 
/var/lib/svn/repos/b1me/products/payone/generic/code/2ds

i want it to become:-

svn+ssh://svn.xxx.com.jo/var/lib/svn/repos/b1me/products/payone/generic/code/core 
svn+ssh://svn.xxx.com.jo/var/lib/svn/repos/b1me/products/payone/generic/code/fees    
svn+ssh://svn.xxx.com.jo/var/lib/svn/repos/b1me/products/payone/generic/code/2ds

in other word i want to add "svn+ssh://svn.xxx.com.jo" at the beginning of each line of this file

like image 884
Osama Ahmad Avatar asked Sep 22 '10 06:09

Osama Ahmad


People also ask

How do you add a string at the beginning of each line in Linux?

The sed command can be used to add any character of your choosing to the beginning of each line. This is the same whether you are adding a character to each line of a text file or standard input.

How do I add text to the beginning of a file in bash?

You cannot insert content at the beginning of a file. The only thing you can do is either replace existing content or append bytes after the current end of file.


2 Answers

One way to do this is to use awk.

awk '{ printf "svn+ssh://svn.xxx.com.jo"; print }' <filename>

If you want to modify the file in place, you can use sed with the -i switch.

sed -i -e 's_.*_svn+ssh://svn.xxx.com.jo&_' <filename>
like image 180
Manoj Govindan Avatar answered Sep 26 '22 01:09

Manoj Govindan


Using sed:

printf "line1\nline2\n" | sed "s/^/new text /"

Using ex:

printf "line1\nline2\n" | ex -s +"%s/^/foo bar /e" +%p -cq! /dev/stdin

Using vim:

printf "line1\nline2\n" | vim - -es +"%s/^/foo bar /e" +%p -cq!

Using shell:

printf "line1\nline2\n" | while read line; do echo foo bar $line; done
like image 30
kenorb Avatar answered Sep 23 '22 01:09

kenorb