首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查找文件中一行前面的字符串的第一次出现

查找文件中一行前面的字符串的第一次出现
EN

Stack Overflow用户
提问于 2022-07-20 15:54:15
回答 2查看 48关注 0票数 0

我有一个文件格式化为:

代码语言:javascript
复制
$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的第一列如下:

代码语言:javascript
复制
gcbox1.org.rpz.my.gc_domain.com
gcbox2.org.rpz.my.gc_domain.com

AWS看起来应该是:

代码语言:javascript
复制
awsbox1.rpz.my.aws_domain.com
awsbox2.rpz.my.aws_domain.com
awsbox3.rpz.my.aws_domain.com

我把文件读成:

代码语言:javascript
复制
f = open('current_records', 'r')
records_string = f.readlines()

如何在不捕获文件中的每个$ORIGIN行的情况下找到前面的$ORIGIN行及其rpz.*子字符串?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-07-20 17:31:47

当遇到新的origin行时,我会逐行读取文件并更新变量$ORIGIN的值。

还有一些小的调整,我将通过评论相关的说明来解决。

代码语言:javascript
复制
# 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‘字段’的新文件。

应该注意的是,如果不能期望文件结构完全相同,则此解决方案将无法工作。另外,您可能需要处理我在您的帖子中看不到的任何额外的空白空间。

票数 0
EN

Stack Overflow用户

发布于 2022-07-20 16:14:44

代码语言:javascript
复制
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']]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73054705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档