How to concatenate files with the same prefix (and many prefixes)?

后端 未结 4 847
死守一世寂寞
死守一世寂寞 2021-01-15 23:21

I have many files that have the same prefix, only the bit after the underscore is different. And I have many prefixes as well! Underscore does not appear anywhere else in th

4条回答
  •  渐次进展
    2021-01-15 23:53

    I had a similar problem, had many files and wanted to group and cat them by prefix, I used this little script:

    ls | awk -F '_' '!x[$1]++{print $1}' | while read -r line
    do
        cat $line* > all_$line\.txt
    done
    

    ls will show all the files in the directory

    In awk the -F '_' option is to set the underscore as the delimiter, and the code itself acts like uniq, meaning will print each prefix only once.

    Then we run a loop on all prefixes and cat all the files with the same prefix.

提交回复
热议问题