Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8178781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:50:53+00:00 2026-06-06T23:50:53+00:00

I have a script, which I want to use to remove a line that

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:50:55+00:00Added an answer on June 6, 2026 at 11:50 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Makefile from which I want to call another external bash script
I have a Perl script which I want to run every 4 hours through
I have a script in which I read html files which I want to
I have a form which on submit I want to run a script via
I have a script which similar to this: foo.php class Foo { function Foo()
I have a backup script which copies a file via ssh and creates a
i need help in bash script I have a lic.txt at domain.com which contains
I have a script that compiles an .exe file and runs the --tree=all of
I have script which allows to display Bing search results. I can call for
i have a script which is for virtual keyboard, i am facing some problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.