Linux 如何在每行的开头添加添加文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3766801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:14:34  来源:igfitidea点击:

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

linuxcommand-linefile

提问by Osama Ahmad

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”

采纳答案by Manoj Govindan

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 sedwith the -iswitch.

如果要就地修改文件,可以sed配合-i开关使用。

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

回答by ghostdog74

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

回答by hluk

Simple way:

简单的方法:

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

回答by Majid Azimi

It can also be done with Perl:

也可以用 Perl 来完成:

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

回答by kenorb

Using sed:

使用sed

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

Using ex:

使用ex

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

Using vim:

使用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