sed错误,抱怨“命令后有额外字符”

8
#!/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

我的问题是:如何处理$PWD的情况?
1个回答

8
为了使用备用分隔符来处理地址,您需要使用反斜杠 - \
sed "\:$PWD:d" < $myfile

应该可以工作。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接