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

后端 未结 4 856
死守一世寂寞
死守一世寂寞 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-16 00:09

    I had to do something very similar and I don't feel like the previous answers here solve your problem as they require a huge amount of manual input if there are many different prefixes, not just a few prefixes with lots of files all with the same prefix. If I knew the pattern of your prefix I could give you more specific advice, but for now I'm just going to assume that your prefix is numbering with leading zeros (as it is with my files). I am going to assume the following, but they need not be true to work:

    ~/test01/001-test.txt
    ~/test01/002-test.txt
    ~/test01/003-test.txt
    
    ~/test02/001-test.txt
    ~/test02/002-test.txt
    ~/test02/003-test.txt
    

    Once this is set up I'm going to change into a merge directory where I want all my merged files to be written to and then run the cat command in a for loop.

    cd ~/merge
    
    for i in {001..003}; do cat ../test*/"$i"*.txt > "$i"-merge.txt ; done
    

    This will use 001, 002, and 003 as prefixes and look in all of the test directories for files that match these prefixes and merge them together in the order they're found. The end result will appear in:

    ~/merge/001-merge.txt
    ~/merge/002-merge.txt
    ~/merge/003-merge.txt
    

    I know this is a lot late, but hopefully it helps someone else. I have to do this with 5000 prefixes, so I completely understand.

提交回复
热议问题