我试图使用sed从文件/root/.ssh/authorized_keys
中删除给定行$ 2。这是我正在运行的命令,不幸的是,它并未删除该行。任何想法我做错了什么?使用Sed从文本文件中删除一行
sed -i '/$2/d' /root/.ssh/authorized_keys
我试图使用sed从文件/root/.ssh/authorized_keys
中删除给定行$ 2。这是我正在运行的命令,不幸的是,它并未删除该行。任何想法我做错了什么?使用Sed从文本文件中删除一行
sed -i '/$2/d' /root/.ssh/authorized_keys
您还可以使用shell
while read -r line
do
case "$line" in
*"$2"*) continue;;
*) echo "$line" ;;
esac
done </root/.ssh/authorized_keys> tempfile
mv tempfile /root/.ssh/authorized_keys
更像'fgrep -v“$ 2”authorized_keys' ...! – tripleee
对于那些冷静下来的人,确保你知道你为何倒下。 –
@tripleee,你可能想把这个评论作为答案。这是我在这里看到的最好的答案。 –
发布非sed的答案由格伦·杰克曼的建议。 Tom Zych已经发布了基本的“正确答案”;还请注意holygeek的评论。
mv authorized_keys authorized_keys~
fgrep -v "$2" authorized_keys~ >authorized_keys
你或许可以没有临时文件,但我不在我的电脑上,所以我现在玩的很安全。
您是否看到错误讯息?你有写入权限的文件? –