Join all files in a directory

后端 未结 4 980
无人共我
无人共我 2021-01-15 05:51

How can I join all of the files in a directory. I can do it in one step by explicitly naming the files below, is there a way to do it without explicitly naming the files?

相关标签:
4条回答
  • 2021-01-15 06:40

    use awk, say you want to join on 1st field

    awk '{a[$1]=a[$1] FS $0}END{for(i in a) print i,a[i]}' file*
    
    0 讨论(0)
  • 2021-01-15 06:41

    You can do it by cat ./* >outfile

    0 讨论(0)
  • 2021-01-15 06:47
    #!/bin/bash
    
    data=
    for f in "${rpkmDir}"/HS*.chsn.rpkm
    do
      if [ ! "$data" ]
      then
        data="$(sort "$f")"
        continue
      fi
      data="$(join <(sort "$f") /dev/stdin <<< "$data")"
    done
    echo "$data"
    
    0 讨论(0)
  • 2021-01-15 06:51

    Since the join (in Classic UNIX and under POSIX) is defined so it works on strictly two files at a time, you are going to have to do the iteration yourself, somehow.

    While your notation is marvellously minimal, it is also inscrutable. The chances are that you can use pipes and the fact that '-' as a file name denotes standard input to alter the sequencing, I think. But the hard part is connecting everything together without creating any explicit temporary files. You may be best off simply writing a script that writes your script notation, and feeds that into bash.

    Maybe (untested script):

    cd ${rpkmDir}
    ls HS*.chsn.rpkm |
    {
    read file
    script="sort $file"
    while read file
    do
        script="$script | join - <(sort $file)"
    done
    } | bash
    
    0 讨论(0)
提交回复
热议问题