英文:
Inserting text with sed at start of line gives error
问题
我正在尝试使用sed修改文件,但出现了一个我不理解的错误:
> sed:-e表达式#1,字符0:没有先前的正则表达式
命令:
sed -e 's/^/"asset\//' example.txt
示例文件:
joe
john
peter
sed来自msys2:
which sed
> sed是外部工具:C:\msys64\usr\bin\sed.exe
当在每行末尾添加文本时,以下命令按预期工作:
sed -e 's/$/",/' y.txt
>joe",
john",
peter",
我不明白问题出在哪里。
英文:
I am trying to modify a file with sed but I get an error I do not understand:
> sed: -e expression #1, char 0: no previous regular expression
Command:
sed -e 's/^/"asset\//' example.txt
Example file:
joe
john
peter
sed is from msys2:
which sed
> sed is an external : C:\msys64\usr\bin\sed.exe
When adding text to the end of each line the following works as expected:
sed -e 's/$/",/' y.txt
>joe",
john",
peter",
I am at a lost to understand what is wronghere.
答案1
得分: 1
EDIT(solution on cygwin): 尝试在Cygwin上执行以下操作,我刚刚在系统上测试过,从命令提示符中运行正常。在使用sed
时,如果我使用'
单引号,我会得到与OP得到的相同错误,但如果我在sed
中使用"
,我就能够获得sed
的正确行为(预期的行为是在每一行之前添加"
)。
sed -E "s/^/\"/" Input_file
EDIT2(taken reference from tripleee's comments): 您还可以将您的sed
命令保存到名为file
的文件中,使用命令s/^/\"/
,然后通过以下方式运行sed
代码,我在Cygwin上成功测试过。
sed -f file Input_file
Alternate solution(tested and works on cygwin): 我没有Cygwin,所以无法在上面进行测试,尽管您提到的解决方案对于BASH来说运行良好。您是否可以尝试另一种方法?我刚刚在Cygwin上测试了这个解决方案,结果在那里也正常工作(请参阅EDIT评论)。
sed 's/\(.*\)/"/' Input_file
英文:
EDIT(solution on cygwin): Try following on cygwin sed
, just tested on a system and it worked fine from command prompt. While using '
single quotes with sed
I am getting same error what OP is getting but while using "
in sed
I am able to get correct behavior of sed
(expected one, to add "
before each line)
sed -E "s/^/\"/" Input_file
EDIT2(taken reference from tripleee's comments): You could also save your sed command into a file named file
with command s/^/\"/
then run the sed
code by doing, tested it successfully with cygwin sed
.
sed -f file Input_file
Alternate solution(tested and works on cygwin): I don't have cygwin with me so couldn't test on it, though your mentioned solution works fine for BASH. Could you please try following another approach once? I just tested this solution too on cygwin and it worked fine there (EDIT comment).
sed 's/\(.*\)/"/' Input_file
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论