使用sed [duplicate]删除变量中指定的IP地址

时间:2021-07-19 09:10:02

This question already has an answer here:

这个问题已经有了答案:

I have list of IP:PORT in txt file, they are proxies. I have CASE where i remove it if it's not working (proxy)

我有txt文件中的IP:端口列表,它们是代理。我有这样的情况,如果它不工作,我就删除它(代理)

sed -i "/$proxy[^0-9]/d" proxy.txt

But it's not working. I'm trying to delete IP:PORT (line containing that IP:PORT) specified in variable $proxy, on example 78.102.43.251:8080 with sed Whatever i try I end up deleting multiple lines instead of just one specified in variable. On lines is nothing except IP:PORT Thank you

但这不是工作。我试图删除变量$proxy中指定的IP:PORT(包含该IP:PORT的行),在示例78.102.43.251:8080中使用sed完成。在线上除了IP:PORT谢谢

Content of proxy.txt

proxy.txt内容

79.155.89.121:8080
36.78.131.82:3128
115.84.178.37:3128
183.88.112.146:8080
205.237.163.78:80
162.243.202.25:80
186.219.106.4:8080
154.72.192.154:8080

$proxy variable is defined like this:

$proxy变量的定义如下:

proxy.txt | while read -r proxy; do

1 个解决方案

#1


2  

I have FreeBSD in my machine, and this command works for me:

我的机器里有FreeBSD,这个命令对我很有效:

proxy="152.16.12.15:1515"
sed -i -e "s/$proxy//g" proxy.txt

The above answer replaces $proxy string with blank. It generates blank lines which are not desirable.

上面的答案将$proxy字符串替换为空。它生成不需要的空行。

The working answer (from the comments) is:

工作答案(来自评论)是:

sed -i.bak "s/^$proxy/d" file

#1


2  

I have FreeBSD in my machine, and this command works for me:

我的机器里有FreeBSD,这个命令对我很有效:

proxy="152.16.12.15:1515"
sed -i -e "s/$proxy//g" proxy.txt

The above answer replaces $proxy string with blank. It generates blank lines which are not desirable.

上面的答案将$proxy字符串替换为空。它生成不需要的空行。

The working answer (from the comments) is:

工作答案(来自评论)是:

sed -i.bak "s/^$proxy/d" file