473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

replacing text inplace

Hey All.

I'm learning some python with the seemingly simple task of updating a firewall config file with the new IP address when my dhcpd server hands one out. Yeah, I know it's dangerous to edit such a file "in place" but this is just an exercise at this point. I would not mind using file handles except they seem to add complexity.

The only apparent problem I have with my script so far is that it's adding lots of blank lines to the file when it updates the IP addresses.

There is only one dotted quad that needs changing in a file full of dotted quads and the dhcp lease lasts for months usually so I have no qualms about putting the old IP address in the file, i.e. I won't need to manually update it very often. The address apears on lines that looks like this:

block in from 71.146.250.258/32 to any group 150

So "71.146.250.258" needs to change to "71.146.250.223" or something similar.

Is there a saner, cleaner way to do this that won't add new, blank lines?
Most of the examples on the Intertubes are about multiple files and STDIN.
This is just one file and it needs one string changed on two or three lines.
The script runs from cron, not interactively.

################################################## #########################

import sys
import string
import os
import fileinput
import re

oldip = "71.146.250.258"

ifcfg_lines = os.popen("/sbin/ifconfig eth0").readlines()
newip = string.split(ifcfg_lines[1])[1][-11:]

if newip == oldip:

print "nevermind"

else:

for line in fileinput.FileInput("/etc/ipf.conf",inplace=1):
line = line.replace(oldip,newip)
print line

--
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too dark to read."
-- Groucho Marx

Jun 27 '08 #1
1 1626
On 2008-04-20, Matt Herzog <ms*@blisses.orgwrote:
I'm learning some python with the seemingly simple task of
updating a firewall config file with the new IP address when
my dhcpd server hands one out. Yeah, I know it's dangerous to
edit such a file "in place"
I don't see how what you're doing is editing a file "in place".
but this is just an exercise at this point. I would not mind
using file handles except they seem to add complexity.
Do you mean file objects?
The only apparent problem I have with my script so far is that
it's adding lots of blank lines to the file when it updates
the IP addresses.
When you do this:

for line in inputfile:

Each instance of 'line' has a newline at the end of it.

When you do this:

print line

The print operation adds another newline. Try it this way:

print line,

The comma tells print not to append a newline after it has
printed "line".
So "71.146.250.258" needs to change to "71.146.250.223" or something similar.

Is there a saner, cleaner way to do this that won't add new,
blank lines?
sed -i s/71.146.250.258/71.146.250.223/g filename

I suppose one probably should escape the dots in the input
regex...

--
Grant Edwards grante Yow! With YOU, I can be
at MYSELF ... We don't NEED
visi.com Dan Rather ...
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Anna Hutter | last post by:
Hello, this question is about DB2 UDB V8 and its inplace reorganisation. It seems that in small tables, an inplace reorg doesn´t start. Is there a limit in size of a table when a reorg starts?...
2
by: sethwai | last post by:
Hi, I have a nightly script that executes inplace reorgs allow write access for several tables after a previous script does a large number of delete operations. It usually has been executing...
2
by: aj | last post by:
DB2 LUW v8.2 FP 14 RHAS 2.1 I'm confused about the NOTRUNCATE TABLE option of the REORG TABLE INPLACE command. In my mind, truncating the table means that you delete all rows that it has - why...
1
by: nitinloml | last post by:
#!/usr/bin/python import string import re import random import sys import time import fileinput tn = time.time() t = str(tn)
0
by: Roger | last post by:
I changed my classic reorgs to inplace reorg on my siebel database last weekend and ran into the log space getting full issue . This is one of the command on my script : reorg table SIEBEL.ASSET...
1
by: varsha21 | last post by:
Hi, I am writing a command line perl program to convert a file in dos format to unix format. I know that the following instruction executes successfully on one of the unix machine. I am trying...
0
by: Gabriel Genellina | last post by:
En Thu, 08 May 2008 09:11:56 -0300, Michael Mabin <d3vvnull@gmail.comescribió: Like this? <code> import sys,fileinput for line in fileinput.input(inplace=True):...
0
by: mvsmsh | last post by:
Hi listers , Has anyone met REORG INPLACE error -964 " The transaction log for the database is full " ? I wonder , why REORG INPLACE error -964 even if archive successful ? We're...
6
by: Ramesh | last post by:
Hello, I am using the ofstream class to create a text file with keys and values like: Key1=Value10 Key2=Value15 Key3=Value20 In case I need to set a new value for Key2, say value50 - I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.