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

时间:2014-06-17 20:44:37

标签: bash shell sed

#!/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 个答案:

答案 0 :(得分:6)

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

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

应该工作。

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