我如何在每行的开头添加添加文字?

时间:2022-06-01 18:23:47

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

换句话说我想在这个文件的每一行的开头添加“svn + ssh://svn.xxx.com.jo”

5 个解决方案

#1


7  

One way to do this is to use awk.

一种方法是使用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开关一起使用。

sed -i -e 's_.*_svn+ssh://svn.xxx.com.jo&_' <filename>

#2


2  

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

#3


0  

ruby -pne 'sub(/^/,"svn+ssh://svn.xxx.com.jo")' file

#4


0  

Simple way:

sed -i 's_._svn+ssh://svn.xxx.com.jo_' <filename>

#5


0  

It can also be done with Perl:

它也可以用Perl完成:

perl -pe 's#^#svn+ssh://svn.xxx.com.jo#' input.file

#1


7  

One way to do this is to use awk.

一种方法是使用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开关一起使用。

sed -i -e 's_.*_svn+ssh://svn.xxx.com.jo&_' <filename>

#2


2  

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

#3


0  

ruby -pne 'sub(/^/,"svn+ssh://svn.xxx.com.jo")' file

#4


0  

Simple way:

sed -i 's_._svn+ssh://svn.xxx.com.jo_' <filename>

#5


0  

It can also be done with Perl:

它也可以用Perl完成:

perl -pe 's#^#svn+ssh://svn.xxx.com.jo#' input.file