bash sed 错误抱怨“命令后的额外字符”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24272961/
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-09-18 10:42:03  来源:igfitidea点击:

sed error complaining "extra characters after command"

bashshellsed

提问by Daniel

#!/bin/bash

# Let's say now, we are working in my $HOME directory

# Content of testfile (originally)
# 123456
# ABCDEF
# /home/superman

string="ABCDEF"
myfile="$HOME/testfile"

# test-1, this is okay
sed -i "/$string/d" $myfile
echo $string >> $myfile

# test-2, this fails
# ERROR (sed: -e expression #1, char 4: extra characters after command)
sed -i "/$PWD/d" $myfile
echo $PWD >> $myfile

# Not working either
sed -i ":$PWD:d" $myfile
echo $PWD >> $myfile

My question: How to handle the $PWD situation?

我的问题:如何处理 $PWD 的情况?

回答by evil otto

To use the alternate delimiters for addresses, you need to use backslash - \

要使用地址的备用分隔符,您需要使用反斜杠 - \

sed "\:$PWD:d" < $myfile

Should work.

应该管用。

Of course for this exact example, grep -vis probably easier.

当然对于这个确切的例子,grep -v可能更容易。