这应该很容易。我会使用Python,因为我是一名Python粉丝。大纲:
你真的无法编辑就地文件...嗯,我想你可以,如果每一个新的变量名总是一模一样的长度与旧名称。但为了便于编程和运行时的安全性,最好总是编写新的输出文件,然后删除原文。这意味着在运行此操作之前,您至少需要20 GB的可用磁盘空间,但这应该不成问题。
这是一个Python程序,显示如何做到这一点。我用你的示例数据来制作测试文件,这似乎工作。
#!/usr/bin/python
import re
import sys
try:
fname_idmap, fname_in, fname_out = sys.argv[1:]
except ValueError:
print("Usage: remap_ids <id_map_file> <input_file> <output_file>")
sys.exit(1)
# pattern to match an ID, only as a complete word (do not match inside another id)
# match start of line or whitespace, then match non-period until a period is seen
pat_id = re.compile("(^|\s)([^.]+).")
idmap = {}
def remap_id(m):
before_word = m.group(1)
word = m.group(2)
if word in idmap:
return before_word + idmap[word] + "."
else:
return m.group(0) # return full matched string unchanged
def replace_ids(line, idmap):
return re.sub(pat_id, remap_id, line)
with open(fname_idmap, "r") as f:
next(f) # discard first line with column header: "oldId newIds"
for line in f:
key, value = line.split()
idmap[key] = value
with open(fname_in, "r") as f_in, open(fname_out, "w") as f_out:
for line in f_in:
line = replace_ids(line, idmap)
f_out.write(line)
非常感谢你!我将把这段代码展示给我的程序员。谢谢 –