gpt4 book ai didi

linux - 水平合并多个文件中的列

转载 作者:太空宇宙 更新时间:2023-11-04 03:51:20 26 4
gpt4 key购买 nike

我尝试编写一个脚本,该脚本从多个文件中各获取两列,并将它们水平连接在一起。问题是,列的内容在文件中的顺序不同,因此在连接之前需要对数据进行排序。

这是我到目前为止所想到的:

!/bin/bash

ls *.txt > list

while read line; do
awk '{print $2}' "$line" > f1
awk '{print $8}' "$line" > f2
paste f1 f2 | sort > "$line".output
done < list

ls *.output > list2

head -n 1 list2 > start

while read line; do
cat "$line" > output
done < start

tail -n +2 list2 > list3

while read line; do
paste output "$line" | cat > output
done < list3

我的编程可能不是那么高效,但它做了我想要它做的事情,除了倒数第二行,它没有正确地将文件连接在一起。如果我在命令行中输入该行,它可以正常工作,但在 while 循环中它会丢失列。

数据文件如下所示:

bundle_id   target_id   length  eff_length  tot_counts  uniq_counts est_counts  eff_counts  ambig_distr_alpha   ambig_distr_beta    fpkm    fpkm_conf_low   fpkm_conf_high  solvable    tpm
1 comp165370_c0_seq1 297 0.000000 0 0 0.000000 0.000000 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 F 0.000000e+00
2 comp75418_c0_seq1 1371 852.132325 35 0 0.005490 0.008832 8.287807e-04 5.283100e+00 4.583199e-04 0.000000e+00 2.425095e-02 T 6.225299e-04
3 comp76235_c0_seq1 1371 871.645349 44 9 43.994510 69.198412 2.002884e+00 3.142003e-04 3.590738e+00 3.516301e+00 3.665174e+00 T 4.877251e+00
4 comp31034_c0_seq1 379 251.335522 14 0 7.049180 10.629771 1.000000e+00 1.000000e+00 1.995307e+00 0.000000e+00 5.957982e+00 F 2.710199e+00
5 comp36102_c0_seq1 379 234.689179 14 0 6.950820 11.224893 1.000000e+00 1.000000e+00 2.107017e+00 0.000000e+00 6.350761e+00 F 2.861933e+00
6 comp26522_c0_seq1 220 0.000000 0 0 0.000000 0.000000 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 F 0.000000e+00
7 comp122428_c0_seq1 624 0.000000 0 0 0.000000 0.000000 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 F 0.000000e+00

我需要 target_id 和 eff_counts 列。

这不是完整的问题,但我想我应该从小处开始。后来我希望目标 ID 在开始时只出现一次。我希望新文件中有一个标题,其中包含对特定列有贡献的文件的名称。

target_id             file_1        file_2        file_3
comp26522_c0_seq1 0.000000 [number] [number]
comp31034_c0_seq1 10.629771 [number] [number]
comp36102_c0_seq1 11.224893 [number] [number]
comp75418_c0_seq1 0.008832 [number] [number]
comp76235_c0_seq1 69.198412 [number] [number]
comp122428_c0_seq1 0.000000 [number] [number]
comp165370_c0_seq1 0.000000 [number] [number]

编辑:我在示例中添加了更多信息。 [数字]只是占位符;实际上,它们的数字与 file_1 下的行类似。此外,标题“file_1”将是输入文件的名称。并且 target_id 应该被排序。所有文件应包含相同的 target_ids,但顺序不同。

编辑二:输出

我用四个文件对其进行了测试,输出如下所示:

    comp0_c0_seq1   0.000000
comp100000_c0_seq1 1.919404
comp100002_c0_seq1 2.118776
comp100003_c0_seq1 0.072916
comp100004_c0_seq1 0.000000
comp100005_c0_seq1 0.000000
comp100006_c0_seq1 1.548160
comp100007_c0_seq1 7.616481
comp100008_c0_seq1 0.000000
comp100009_c0_seq1 1.374209

第一列的左侧有一个空列,其中包含数据。并且仅存在最后一个文件中的数据。

感谢您的帮助!

更新:

我解决了倒数第二行的问题。这是我使用的代码:

while read line; do
join output "$line" > output2
cat output2 > output
done < list3

这是输出:

comp0_c0_seq1      0.000000 0.000000 0.000000 0.000000
comp100000_c0_seq1 1.919404 1.919404 0.000000 1.919404
comp100002_c0_seq1 2.118776 2.118776 2.225852 2.118776
comp100003_c0_seq1 0.072916 0.072916 1.228136 0.072916
comp100004_c0_seq1 0.000000 0.000000 0.000000 0.000000
comp100005_c0_seq1 0.000000 0.000000 1.982851 0.000000
comp100006_c0_seq1 1.548160 1.548160 1.902749 1.548160
comp100007_c0_seq1 7.616481 7.616481 0.000000 7.616481
comp100008_c0_seq1 0.000000 0.000000 0.000000 0.000000
comp100009_c0_seq1 1.374209 1.374209 1.378667 1.374209

现在我只需要弄清楚如何将包含所有文件名的 header 添加到文件顶部。

最佳答案

经过大量阅读和测试,我终于想出了一个完全符合我要求的脚本。

这可能不是 bash 最有效的使用方式,但它工作得很好。

ls *.xprs > list

while read line; do
echo "parsing $line"
awk '{print $2}' "$line" > f1
awk '{print $8}' "$line" > f2
paste f1 f2 | sort | head -n -1 > "$line".output
done < list

ls *.output > list2

head -n 1 list2 > start

while read line; do
cat "$line" > output
done < start

tail -n +2 list2 > list3

while read line; do
join output "$line" > output2 2>/dev/null
cat output2 > output
done < list3
sed '1i Contig_ID' list2 | awk '{printf("%s ", $0)}' | sed -e '$a\' | sed 's/.xprs.output//g' > list4

cat list4 output > results.txt

关于linux - 水平合并多个文件中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26274349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com