tags:

views:

201

answers:

3

After looking at this post, it looks like I can just use cat to merge files.

However, I am a bit confused on how to do this with my array of filename prefixes. For example:

prefixes=( pre1 pre2 pre3 pre4 pre5 )

If I have an array of prefixes like that, how can I make a command to look like this or do something similar to this:

cat pre1.file pre2.file pre3.file pre4.file pre5.file > merged.file
+3  A: 

You can use a loop to iterate over the file names in the array:

prefixes=( pre1 pre2 pre3 pre4 pre5 )
for p in "${prefixes[@]}"; do cat "$p.file"; done > merged.file
sth
A: 
eval "cat {${prefixes[*]}}.file > merged.file"
David Zaslavsky
This doesn't work for me. The outer braces are keep messing up with the file name and gets included with the name, such as {pre}.file. I tried taking them out, but it still didn't work.
bLee
A: 

If all your prefixes follow a pattern then you can do it using globbing:

cat pre*.file > merged.file

or

cat pre?.file > merged.file

Also, you can use brace expansion for a list of prefixes:

cat {pre1,pre2,pre3,pre4,pre5}.file > merged.file
Dennis Williamson