如何在大列表中添加前缀?

时间:2014-10-09 17:11:34

标签: linux sed cygwin prefix

我想为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执行此操作。

2 个答案:

答案 0 :(得分:2)

出于某种原因,它不适用于Cygwin,但我在Virtualbox上安装了一个Linux,它使用了原始命令:

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

感谢您的回答。

答案 1 :(得分:0)

以下是您正在寻找的答案:

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

这里的关键点是&表示在正则表达式的第一部分中标识的元素(即,使用&符号&重复正斜杠之前的任何内容)

测试 - 在原始字符串之前和之前插入字符:

[themarcelor@linuxbox tmp]$ touch test.txt
[themarcelor@linuxbox tmp]$ echo "line1" >> test.txt
[themarcelor@linuxbox tmp]$ echo "line2" >> test.txt
[themarcelor@linuxbox tmp]$ echo "line3" >> test.txt
[themarcelor@linuxbox tmp]$ sed 's/^.*$/&@email.com/g' test.txt
line1@email.com
line2@email.com
line3@email.com
[themarcelor@linuxbox tmp]$ sed 's/^.*$/--&/g' test.txt
--line1
--line2
--line3
相关问题