I have 6 directories. Each directory includes 8 text files. These text files share the same prefix. I need to combine these text files as follows;
Assume, first text file in directory_1 contains files containing the text abc_1
, directory_2 contains files with the text abc_2
and so on. Merged text file for the first text files needs to be in this form:
abc_1
abc_2
abc_3
..
abc_6
The second, third... and eighth text files also need to be combined with the same rule.
An implicit method (can fail if unrelated files with filename ending with _ and a number are in the same directory) which names each merged file according to the file number:
An explicit method (safer) using a list of prefixes, which names each merged file according to the file number:
Your files must be sortable, so assuming the names of the files in each directory end with a number from 1 to 8, such as
foo1
,foo2
... in one directory andbar1
,bar2
... in another and so on, you could usecat
and afor
loop to concatenate them:(no need to "quote" a variable we know is a single digit) This will print, in, for example,
newfile1
, the contents ofwhatever those contents are, and in
newfile2
the contents ofdirectory_1/foo2
,diectory_2/bar2
etc