【问题标题】:How to replace multi-column from one text file to a column in another text file?如何将一个文本文件中的多列替换为另一个文本文件中的列?
【发布时间】:2018-12-27 02:05:33
【问题描述】:

我有:

$ cat file1.csv (tab delimited)
R923E06 273911 2990492 2970203 F Resistant 
R923F06 273910 2990492 2970203 F Resistant 
R923H02 273894 2970600 2990171 M Resistant

和:

$ cat file2.txt (space delimited and it's a large file)
R923E06 CC GG TT AA ...
R923F06 GG TT AA CC ...
R923H02 TT GG CC AA ...

如何将file2.txt 中的第一列替换为file1.csv 中的全部6 列?

【问题讨论】:

标签: python bash csv awk sed


【解决方案1】:

看看这个 AWK 示例:

awk 'FNR == NR { d[$1] = $0; next } { $1 = d[$1] } 1' file1.csv file2.txt

在这里,我将file2.txt 中的第一列替换为file1.csv 的对应行(6 列)。

输出:

R923E06 273911 2990492 2970203 F Resistant  CC GG TT AA ...
R923F06 273910 2990492 2970203 F Resistant  GG TT AA CC ...
R923H02 273894 2970600 2990171 M Resistant  TT GG CC AA ...

如果您希望结果中的所有内容都以制表符分隔,您可以添加gsub(/[[:space:]]/,"\t") 以将任何空格或制表符替换为制表符:

awk 'FNR == NR { d[$1] = $0; next } { $1 = d[$1]; gsub(/[[:space:]]/,"\t") } 1' file1.csv file2.txt

【讨论】:

  • 我使用你的 awk,但它让我低于错误“ awk:超出程序限制:最大字段数 size=32767 FILENAME="rom_ped2" FNR=1 NR=178",我尝试安装并使用 gawk 但我使用 Ubuntu 12.04 我认为我正在寻找的包不存在。所以我正在寻找 Python 脚本来做到这一点,任何人都有 sloution?
  • @mary,嗯,那是非常过时的 Ubuntu 版本……你可以看看这个:askubuntu.com/questions/244268/…
【解决方案2】:

使用join 你可以这样做:

join   <(sed -e 's/\t/ /g' file1.csv) <(cat file2.txt)

sed 将制表符更改为空格

join 将两个文件的行连接到一个公共字段中。

输出:

R923E06 273911 2990492 2970203 F Resistant  CC GG TT AA ...
R923F06 273910 2990492 2970203 F Resistant  GG TT AA CC ...
R923H02 273894 2970600 2990171 M Resistant TT GG CC AA ...

【讨论】:

  • 两个文件的顺序(对于车牌号,即R923E06)不一样,剂量联合注意这个注释吗?我忘记写的其他事情是,在将 6 列添加到 file2.txt 之后,我也必须分离基因型(即 R923E06 273911 2990492 2970203 F 抗性 C C G G T T A A ...)
  • 如果没有排序,你可以试试这个:join &lt;(sed -e 's/\t/ /g' file1.csv | sort -k 1) &lt;(sort -k 1 file2.txt) sort 是按第一个字段排序的。
  • 您的命令适用于上述数据,但当我用于类似数据时,它不起作用。两个文件的第一行是: cat sample1.txt > 1 1011001 1001164 981328 1 -9 和 cat sample2.txt> 1011001 A G G G G G C C A A ...我想要 sample1.txt 的 6 列替换 sample2.txt 的第一列
  • 由于某种原因,命令以一种奇怪的格式粘贴,正确的命令是:join -1 2 -2 1 &lt;(sed -e 's/\t/ /g' sample1.txt | sort -k 1) &lt;(sort -k 1 sample2.txt) | awk '{t = $1; $1 = $2 ; $2 = t;}1'
  • @mary 如果您觉得有帮助,请记得接受答案。谢谢
【解决方案3】:
#import pandas
import pandas as pd

#read file1.csv
#set index_col as false if file has delimiters at the end
file1 = pd.read_csv( 'file1.csv', ' ', index_col = False, names = 
['1','2','3','4','5','6']);

#read file2.txt, read_csv can read txt files as well
#set index_col as false if file has delimiters at the end
file2 = pd.read_csv( 'file2.csv', ' ', index_col = False, names = 
['1','2','3','4','5']);

#drop first column
file2.drop( '1', axis = 1, inplace = True )

#concat both frames
final = pd.concat([file1, file2], axis = 1)
#you might end up with mixed column names you can change it by using 
final.columns = ['col1', 'col2', ....]


#save as csv
final.to_csv('out.csv',sep='\t')

【讨论】:

  • 请使用一些解释/描述,以便更容易理解,谢谢!!
猜你喜欢
  • 2013-05-19
  • 2018-03-29
  • 2019-03-06
  • 2014-12-18
  • 2016-07-12
  • 2011-12-12
  • 2020-01-09
  • 2023-03-07
  • 1970-01-01
相关资源
最近更新 更多