简体   繁体   English

在Unix中加入多个文件

[英]joining multiple files in unix

I have multiples files in a folder containing keys and values separated by space along with a file containing only key values. 我的文件夹中有多个文件,其中包含键和值(以空格分隔)以及仅包含键值的文件。 All files are sorted according to keys. 所有文件均按键排序。 All have the same keys. 全部具有相同的键。 (no missing keys also). (也没有丢失的键)。 I want to have a file with key followed by all the values (values from same file into same column) 我想要一个带有的文件, 后跟所有值 (从同一文件到同一列的值)

key file looks like follows : 密钥文件如下所示:

00001740-a

00001740-n

00001740-r

00001740-v

00001837-r

00001930-n

00001981-r

00002098-a

rest files look like this : 其余文件如下所示:

00001740-a      5.21718e-05

00001740-n      3.32329e-05

00001740-r      4.5483e-06

00001740-v      7.54663e-05

00001837-r      8.79043e-06

00001930-n      3.75099e-06

00001981-r      1.4668e-06

00002098-a      3.18465e-06

I couldn't find anything on join man page. 我在加入手册页上找不到任何内容。 Please help me out here. 请帮我在这里。

man join:

NAME
       join - join lines of two files on a common field

SYNOPSIS join [OPTION]... FILE1 FILE2

Update - I wrote a shell script to generate the command mentioned as one of the answers and outputted it to another shell file and then executed it. 更新-我编写了一个Shell脚本来生成作为答案之一提到的命令,并将其输出到另一个Shell文件中,然后执行它。 any better ideas? 还有更好的主意吗?

 #!/bin/bash
 echo -n "paste offsets.txt "
 for f in *.ppv
 do
     echo -n " <(cut -f2 "$f")"
 done

请尝试以下命令:

join FILE1 FILE2 | join - FILE3 | join - FILE4

怎么样:

paste keyfile <(cut -d' ' -f2 file1) <(cut -d' ' -f2 file2) ... <(cut -d' ' -f2 fileN)

If the data is small enough to fit in memory, try: 如果数据足够小以适合内存,请尝试:

awk 'NF > 1{ a[$1] = a[$1] " " $2} END {for( i in a ) print i, a[i]}' *.ppv

This will output the keys in a different order, so you may want to pipe the output to sort . 这将以不同的顺序输出键,因此您可能希望通过管道将输出sort

You can use "eval" to execute a command, for example: 您可以使用“ eval”执行命令,例如:

#!/bin/bash
first="1"
for f in *.ppv
do
    if [[ ${first} -eq "1" ]]; then
        command="join offset.txt ${f}"
        first="0"
    else
        command="${command} | join - ${f}"
    fi
done
eval ${command}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM