简体   繁体   中英

Remove line containing specific string

I have a script, which I want to use to remove a line that contains a specific IP address from a file. This is:

for line in fileinput.input(hostsFileLoc,inplace =1):
    line = line.strip()
    if not hostIP in line:
        print line

This sort of works, however, there are two things I am trying to work out.

  1. This script will remove any matches, so in example before

    127.0.0.1
    127.0.0.11
    127.0.0.111
    192.168.0.1

    If I run this with the input of "127.0.0.11", it will remove both "127.0.0.11" and "127.0.0.111", which is not what I want.

  2. This script doesn't handle csv files either. I need to remove it from a file where each line is just and IP address per line, as per the list above, and also a csv file where the first field is the offending IP address. I have tried using the regex [\\s\\,]+ in the strip function, but this doesn't work correctly and adds a blank line when it reprints the remaining lines back to file.

I know this may be a lot to ask, but I am still trying to find my way around the wonders of Python.

You just need to be more specific in your test. For the first example test that the whole line (after a strip) is equal to the ip address (stricter than that it contains it - thus working around your first issue). For the second, split each line on the comma character, and test that the first element is equal to your IP address string.

So for the first file type:

for line in fileinput.input(hostsFileLoc,inplace =1):
    line = line.strip()
    if line != hostIP:
        sys.stdout.write(line)

And for the csv file type:

for line in fileinput.input(hostsFileLoc,inplace =1):
    elements = line.split(",")
    first = elements[0].strip()
    if first != hostIP:
        sys.stdout.write(line)

If you strip the string, you can just test if they are equal or not, instead of if it contains the input.

for line in fileinput.input(hostsFileLoc,inplace =1):
    line = line.strip()
    # Only print if they are not the same
    if hostIP != line:
        print line
for line in fileinput.input(hostsFileLoc,inplace =1):
    line = line.strip()
    if hostIP!=line:
        print line

And for csv files:

for line in fileinput.input(hostsFileLoc,inplace =1):
    line = line.strip()
    if not line.startswith(hostIP+','):
        print line

I would use a regex to extract the IP address from the line and check it against the IP you are looking for.

ValidIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"

http://docs.python.org/library/re.html

This might be too much for such a simple task but otherwise, you will have to handle every edge case seperatly which might produce a dirty and complicated code. Specially if you have to add other filters in the future (Like hotnames for example).

As for the CSV file, why don't you simply parse the file and remove whatever entry you want?

http://docs.python.org/library/csv.html

So which lines we do not want? Those lines that contain the ip-address!

The regular expression for this is r'127\\.0\\.0\\.1' . We have to escape the dots ( \\. ) to make them literal dots because a dot has special meaning in regular expressions.

But we mean only those lines where the address is at the beginning ( ^ ) or after a non-digit ( \\D ) and if it is followed by the end of the line ( $ ) or a non-digit.

That makes: r'(^|\\D)127\\.0\\.0\\.1(\\D|$)'

import re
regexp = re.compile( r'(^|\D)' + re.escape( '127.0.0.1' ) + r'(\D|$)' )

for line in fileinput.input(hostsFileLoc,inplace =1):
    line = line.strip()
    if not regexp.search( line ):
        print line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM