bash script - iterative merge files on a directory

你离开我真会死。 提交于 2019-12-13 04:48:22

问题


Hello I want to iteratively merge the files in input_directory and put the merged ones in output_directory.

Suppose in input_directory I have : file1.txt, file2.txt, file3.txt

I want to the output directory to contain the following files:

  • merge1.txt : same as file1.txt
  • merge2.txt : merge file1.txt file2.txt
  • merge3.txt : merge file1.txt file2.txt file3.txt

I have almost no experience with bash scripts, it is obvious that it can be done with an iterator in a for loop, but I don't know how.

Thank you in advance..


回答1:


Store the previous filename that was appended to output and use it during next iteration:

#!/bin/bash

touch prev #a temp variable to hold previous filename
i=1;

for file in $(ls /INPUT/file*); 
do 
   cat /OUTPUT/$prev /INPUT/$file > /OUTPUT/merge$i;
   prev=merge$i;
   let i=i+1
 done

This code takes advantage of the fact ls list the files in sorted when they have same prefix. You can modify it if you have filenames with different prefixes and have a specific order in which you want to output them.



来源:https://stackoverflow.com/questions/13078900/bash-script-iterative-merge-files-on-a-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!