Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 交换文件中的列位置_Python_Unix_Awk - Fatal编程技术网

Python 交换文件中的列位置

Python 交换文件中的列位置,python,unix,awk,Python,Unix,Awk,我有一个类似以下内容的文件: #name cdsStart cdsEnd exonCount exonStarts exonEnds NM_017436 431 586 3 420,440,513, 435,500,596, NM_001173466 720 950 4 700,752,821,823, 721,760,900,973, 我想做的是将第2列和第3列中的数字分别替换为第5列中的第一个数字和第6列中的最后一个

我有一个类似以下内容的文件:

#name   cdsStart    cdsEnd  exonCount   exonStarts  exonEnds
NM_017436   431    586    3   420,440,513,    435,500,596,
NM_001173466    720    950    4    700,752,821,823,    721,760,900,973,
我想做的是将第2列和第3列中的数字分别替换为第5列中的第一个数字和第6列中的最后一个数字。这是我想要的输出:

NM_017436   431    586    3   *431*,440,513,    435,500,*586*,
NM_001173466    720    950    4    *720*,752,821,823,    721,760,900,*950*,
为了清楚起见,我在输出文件中用星号标出了更改。我想做的是用以下脚本拆分文件:

with open('nonsensepositions.txt') as inf:
    with open('nonsensepositions_split.txt', 'w') as outf:
        for line in inf:
            outf.write('\t'.join(line.split(',')))

然后尝试交换特定的列,但我认为这可能是一个挑战,因为拆分后的列数量不同。然后,我必须找到一种方法,在执行交换后使其显示为原始文件。有没有更简单的方法来执行这种类型的交换,或者我需要根据
分割文件并按照我的建议执行?

我喜欢awk。现在我只需要知道如何使用sub。谢谢。
sub(/old/,“new”,target)
将变量
target
中的regexp
old
替换为字符串
new
。要学习awk,请获取Arnold Robbins的《有效的awk编程》,第四版。谢谢。我得研究一下如何得到那本书。
$ awk '{ sub(/^[0-9]+/,$2,$5); sub(/[0-9]+,$/,$3",",$6) } 1' file
#name   cdsStart    cdsEnd  exonCount   exonStarts  exonEnds
NM_017436 431 586 3 431,440,513, 435,500,586,
NM_001173466 720 950 4 720,752,821,823, 721,760,900,950,