使用 Sed 从文本文件中删除一行
我正在尝试使用 sed 从文件 /root/.ssh/authorized_keys
中删除给定的行 $2 。这是我正在运行的命令,不幸的是,它没有删除该行。知道我做错了什么吗?
sed -i '/$2/d' /root/.ssh/authorized_keys
I am trying to remove a given line $2 from the file /root/.ssh/authorized_keys
using sed. Here is the command I am running, unfortunately, its not deleting the line. Any idea what I'm doing wrong?
sed -i '/$2/d' /root/.ssh/authorized_keys
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$2
不会被单引号内的值替换。应该使用双引号。$2
won't be replaced with its value inside single quotes. Should work with double quotes.按照 Glenn Jackman 的建议发布非 sed 答案。 Tom Zych 已经发布了基本的“正确答案”;另请注意 Holygeek 的评论。
如果没有临时文件,您可能也能过得去,但我不在电脑前,所以我现在要谨慎行事。
Posting a non-sed answer as suggested by Glenn Jackman. The basic "right answer" was already posted by Tom Zych; note also the comment by holygeek.
You could probably get by without the temporary file but I'm not at my computer so I'm playing it safe for now.
您还可以使用外壳
You can also use the shell