首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >大型文件的第一行上的多个替换

大型文件的第一行上的多个替换
EN

Stack Overflow用户
提问于 2012-12-07 15:40:34
回答 2查看 115关注 0票数 1

我有一个很大的文件,第一行是:

公司名称: ref_context repeat_masked s1_smpl_context s1_c_count s1_ct_count s1_non_ct_count s1_m% s1_score s1_snp s1_indels s2_smpl_context s2_c_count s2_ct_count s2_non_ct_count s2_m% s2_score s2_non_ct_count s2_m%s2_score s2_snp s2_m%s2_snp s2_snp s2_m%s2_m%s2_snp s2_ct_count chr En19 en21 en23 en24.

一直到s8。

我希望将s1的所有实例替换为s4,将L1替换为L4,然后将s5s8的所有实例替换为W1W4。最好是用grepawk或其他方式来实现这一点呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-07 15:46:05

仅在文件的第一行中将s[1-4]替换为L[1-4],将s[5-8]替换为W[1-4]

代码语言:javascript
运行
复制
sed -Ee '1s/s([1-4])/L\1/g' -e '1s/s([5-8])/W\1/g' -e '1y/5678/1234/' -e '1q' file

演示:

$ cat文件 启动端链ref_context repeat_masked s1_smpl_context s1_c_count s1_ct_count s1_non_ct_count s1_m% s1_score s1_snp s1_indels s2_smpl_context s2_c_count s2_ct_count s2_non_ct_count s2_m% s2_non_ct_count s2_m% s2_score s2_snp s2_m%s2_m%s2_snp s2_m%s2_snp s2_m%s2_m%s2_snp s2_snp end 20 en22 en23 en25s4_c_count s4_ct_count s4_non_ct_count s4_m% s4_score s4_snp s4_indels s5_smpl_context s5_c_count s5_ct_count s5_non_ct_count s5_m% s5_score s5_snp s5_indels s6_smpl_context s5_indels s6_c_count s5_indels s6_smpl_context s5_indels s6_c_count s5_indels s5_indels s6_c_count s6_c_count s5_indels s6_c_count_count s7_m% s7_score s7_snp s7_indels s8_smpl_context s8_c_count s8_ct_count s8_non_ct_count s8_m% s8_score s8_snp s8_indels s1_line_2 s3_line_3 s8_line_4 $ sed -Ee‘1s/s(1-4)/L/g’-e‘1s/s(5-8)/W1/g’-e '1y/5678/1234/‘-e '1q’文件 启动端链ref_context repeat_masked L1_smpl_context L1_c_count L1_ct_count L1_non_ct_count L1_m% L1_score L1_snp L1_indels L2_smpl_context L2_c_count L2_ct_count L2_non_ct_count L2_m% L2_non_ct_count L2_m% L2_score L2_snp L2_m%L2_m%L2_snp L2_m%L2_snp L2_m%L2_m%L2_snp L2_snp end 20 en22 en23 en25L4_c_count L4_ct_count L4_non_ct_count L4_m% L4_score L4_snp L4_indels W1_smpl_context W1_c_count W1_ct_count W1_non_ct_count W1_m% W1_score W1_snp W1_indels W2_smpl_context W1_indels W2_c_count W1_indels W2_smpl_context W1_indels W2_c_count W1_indels W1_indels W2_c_count W2_c_count W1_indels W2_c_count_count W3_m% W3_score W3_snp W3_indels W4_smpl_context W4_c_count W4_ct_count W4_non_ct_count W4_m% W4_score W4_snp W4_indels

票数 1
EN

Stack Overflow用户

发布于 2012-12-08 13:11:26

由于您的替换没有改变第一行的长度,您还可以选择直接修改原始文件。我不认为您可以使用任何常见的shell命令来完成这个任务,但是您可以用Python编写一个程序来完成这个任务。

然而,我只会考虑这个选项,如果文件是如此巨大,以至于您无法临时复制它。

代码语言:javascript
运行
复制
#!/usr/bin/env python

import os

# On windows, you need to use os.O_RDWR | os.O_BINARY
fd = os.open('modex', os.O_RDWR)
f = os.fdopen(fd)
f.seek(0)
line = f.readline()
replacements = [("s%s" % x, "L%s" % x) for x in range(1,5)] \
        + [("s%s" % (x+4), "W%s" % x) for x in range(1,5)]
for (s,r) in replacements:
    line = line.replace(s, r)
# We cannot use python file objects here, because f.write() _always_ appends,
# regardless of the seek position. So we use the raw object here.
# Mixing raw IO and python IO should be done with care, however we should be
# ok here, as we did not write anything yet.
os.lseek(fd, 0, os.SEEK_SET)
os.write(fd, line)
f.close()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13766237

复制
相关文章

相似问题

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