2014-10-09 76 views
-2

我想为在line.txt中找到的每一行添加一个前缀。
例子:如何将一个前缀添加到一个大列表中?

line1 
line2 
line3 

输出应该是:

--line1 
--line2 
--line3 

我使用的cygwin在cmd中做linux命令:

sed -e 's/^/--/' line.txt > output.txt 

,但我得到这个错误:

sed -e expression #1, char 0: no previous regular expression 

该文件太大而无法用excel打开,所以我需要一种方法来从cmd中执行它。

回答

0

这里就是你正在寻找的答案:

sed 's/^.*$/--&/g' line.txt 

的关键点在这里是&表示在正则表达式(即第一部分标识的元素,无论是重复斜杠前与符号&

测试 - 后和原来的字符串前插入字符:

[[email protected] tmp]$ touch test.txt 
[[email protected] tmp]$ echo "line1" >> test.txt 
[[email protected] tmp]$ echo "line2" >> test.txt 
[[email protected] tmp]$ echo "line3" >> test.txt 
[[email protected] tmp]$ sed 's/^.*$/&@email.com/g' test.txt 
[email protected] 
[email protected] 
[email protected] 
[[email protected] tmp]$ sed 's/^.*$/--&/g' test.txt 
--line1 
--line2 
--line3 
+0

嘿。 它只是说: SED -e表达#1,焦炭8:未结束的 'S' 命令 以下名称系统斜面识别。 – Simon 2014-10-09 17:31:55

+0

我认为这个问题可能是我没有运行linux,但是cygwin。 – Simon 2014-10-09 17:35:33

+0

嗯,可能是因为你正在cygwin上运行这些命令:(尝试删除/ g并查看会发生什么,也可以尝试原始命令,但这次使用/.*/--&/ – theMarceloR 2014-10-09 17:37:40

2

出于某种原因,它不与Cygwin的工作,但我安装在VirtualBox的一个Linux,并将其与原始命令的工作:

sed -e 's/^/--/' line.txt > output.txt 

谢谢你的答案。

相关问题