简体   繁体   中英

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. 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 .

You can use "eval" to execute a command, for example:

#!/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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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