我有一个文件格式化为:
$ORIGIN .com.rpz.my.azure_domain.com
azurebox1 CNAME azurebox1-dr.my.azure_domain.com
$ORIGIN rpz.my.aws_domain.com
awsbox1 CNAME awsbox1-dr.my.aws_domain.com
awsbox2 CNAME awsbox2-dr.my.aws_domain.com
awsbox3 CNAME awsbox3-dr.my.aws_domain.com
$ORIGIN .org.rpz.my.gc_domain.com
gcbox1 CNAME gcbox1-dr.my.gc_domain.com
gcbox2 CNAME gcbox1-dr.my.gc_domain.com对于包含cname记录的每一行,我需要为其前面的$ORIGIN值追加rpz条目。因此,my.gc_domain.com CNAMES的第一列如下:
gcbox1.org.rpz.my.gc_domain.com
gcbox2.org.rpz.my.gc_domain.comAWS看起来应该是:
awsbox1.rpz.my.aws_domain.com
awsbox2.rpz.my.aws_domain.com
awsbox3.rpz.my.aws_domain.com我把文件读成:
f = open('current_records', 'r')
records_string = f.readlines()如何在不捕获文件中的每个$ORIGIN行的情况下找到前面的$ORIGIN行及其rpz.*子字符串?
发布于 2022-07-20 17:31:47
当遇到新的origin行时,我会逐行读取文件并更新变量$ORIGIN的值。
还有一些小的调整,我将通过评论相关的说明来解决。
# read the file
# use the with open scope instead of directly reading the file, so that you won't have to close the stream after.
with open("current_records") as f: # no need for 'r' mode, it's default
records = f.readlines()
# iterate through the list of records and their position indices
# we will need to index the list to replace a CNAME line, since strings are immutable
origin = "" # this will hold the $ORIGIN string
for n, record in enumerate(records):
# if we encounted an $ORIGIN record, we update the variable
if record.startswith("$ORIGIN"):
origin = record.split()[1]
# otherwise, we replace the CNAME value accordingly
else:
# build cname string
cname = record.split()[0] + origin
records[n] = record.replace("CNAME", cname)
# re-build the file and write it
with open("new_records", "w") as f:
f.write("\n".join(records)) # join updated lines interposing new lines between them这将产生一个具有相同结构和更新的CNAME‘字段’的新文件。
应该注意的是,如果不能期望文件结构完全相同,则此解决方案将无法工作。另外,您可能需要处理我在您的帖子中看不到的任何额外的空白空间。
发布于 2022-07-20 16:14:44
import re
data = {}
with open('current_records') as f:
for line in f:
if line.startswith('$ORIGIN'):
curr_type = line[8:]
data[curr_type] = []
else:
data[curr_type].append(line[20:])
out = [[re.sub(r'(\w+)(.*)', r'\1', x)+key for x in value] for key, value in data.items()]
print(out)
# Output:
[['azurebox1.com.rpz.my.azure_domain.com'], ['awsbox1rpz.my.aws_domain.com', 'awsbox2rpz.my.aws_domain.com', 'awsbox3rpz.my.aws_domain.com'], ['gcbox1.org.rpz.my.gc_domain.com', 'gcbox1.org.rpz.my.gc_domain.com']]https://stackoverflow.com/questions/73054705
复制相似问题