2

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)

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

Content of 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.txt | while read -r proxy; do
Darktwen
  • 333
  • 5
  • 18
  • please provide a [mcve] so we can test better – fedorqui Jan 19 '17 at 10:29
  • Sample of what? Proxy .txt? – Darktwen Jan 19 '17 at 10:30
  • Yes, that, so we can see input and expected output – fedorqui Jan 19 '17 at 10:31
  • Done, but I already said it is: IP:PORT on every line, no extra spaces or characters only IP:PORT line by line – Darktwen Jan 19 '17 at 10:32
  • This is how i define variable, if it's of any help proxy.txt | while read -r proxy; do – Darktwen Jan 19 '17 at 10:34
  • note you can test better by just printing those lines that match your condition: `sed -n "/$proxy[^0-9]/p" file`. To me, now it does not match anything if I eg say `proxy="79.155.89.121:8080"`, which is logical because all lines end with the port, with no further content. All in all, it is a bit unclear what you are asking here. What is your final goal? – fedorqui Jan 19 '17 at 10:36
  • Following your example... i have randomly chosen proxy from a file and it is defined like this proxy="79.155.89.121:8080" I already have CASE in bash if it's not working so i issue SED Command REMOVING that ip address from my proxy list, scripts run again. I already have all that i just need SED command to MATCH my $proxy variable which contains IP:PORT correctly. THat's all – Darktwen Jan 19 '17 at 10:39
  • 1
    so you want to remove an entry from `proxy.txt` given in the variable `$proxy`. OK, the command `sed "/$proxy[^0-9]/d"` does not work because there is no need to add that `[^0-9]` to the end if the line just consists in `XXX.XXX.XXX.XXX:NNNN`. Also, it would be benefitial to add `^` before `$proxy` to match the beginning of the line. – fedorqui Jan 19 '17 at 10:43

1 Answers1

2

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

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.

The working answer (from the comments) is:

sed -i.bak "s/^$proxy/d" file
am.rez
  • 876
  • 2
  • 12
  • 24
  • 1
    Why `/g`? why `-e`? – fedorqui Jan 19 '17 at 10:49
  • It's a substitution, searches for a string and changes that with another. `/g` finishes the substitution. – am.rez Jan 19 '17 at 10:54
  • Changes it with // ? So basically you used replace instead of using /d for delete? – Darktwen Jan 19 '17 at 10:54
  • 2
    Yes, my point is that `-e` is an unnecessary extension here. `/g` performs the replacement as many times as possible in the line, while `/` alone does it once, which looks more convenient for this case. Also, this does not remove the line, but _blanks_ it. – fedorqui Jan 19 '17 at 10:55
  • It is replaced by a blank line. I hope you don't mind some empty lines ;) – am.rez Jan 19 '17 at 10:56
  • That is precisely the point of `/d`: it deletes the line :) – fedorqui Jan 19 '17 at 10:56
  • I mind :D Yeah Because random is used and if it chooses blank line then there is no proxy – Darktwen Jan 19 '17 at 10:57
  • The command you gave me @fedorqui sed "/^$proxy/d" proxy.txt just prints out whole file. I do not understand, do i need to add sed -i "/^$proxy/d" proxy.txt ? – Darktwen Jan 19 '17 at 11:01
  • @Darktwen yes, if you want to perform an in-file edition, use `-i`. But just do so when you are sure it works, since your original file will be lost. For safety, add an extension to `-i` like `sed -i.bak "/^$proxy/d" file`, so that a file.bak will be created as a backup of the original file. – fedorqui Jan 19 '17 at 11:04
  • It works with -i, thanks. – Darktwen Jan 19 '17 at 11:06
  • For me it works without `-i`, and errors with it. However without `-i` I need to write the printed result to the file. – am.rez Jan 19 '17 at 11:07
  • 1
    Am.rez feel free to add the `sed -i.bak "s/^$proxy/d" file` solution to your answer if you wish. – fedorqui Jan 19 '17 at 11:18
  • 1
    The `s` is a substitution command, expecting 3 `/`. For a deletion command needs to remove the 's''. – Luka Apr 28 '17 at 16:44
  • @Luka I don't know if the version of `sed` is too old on my FreeBSD 9.2, but it doesn't work for me also. The command I came up with finally is `sed -i '' '/$proxy/d' proxy.txt` – am.rez Apr 29 '17 at 07:59
  • 1
    On Ubuntu 17.04 your command (from answer) is not working (VERSION sed (GNU sed) 4.4). However, `sed -i -e "/^$PROXY/d" file` works just fine. There is no need for S since search requests a parameter to replace with and we just want to delete line. – Luka May 01 '17 at 17:53
  • @Luka it works for both FreeBSD 9.2 (`sed` doesn't show version in BSD systems) and Debian 8.2 (GNU sed 4.2.2). - I can add it to the current solution, or you can write yours :) – am.rez May 02 '17 at 08:20