Hello guys,
I gotta question, i have a lot of log files (simple text) and i need to merge them in group of 10 files, one next to the other, that have sense?
For example, i have the files:
File1
File2
File3
File4
.
.
File100
I need to merge the contents of each file into a new file named Total1, but into Total1 only will be the File1 to File10, then will be another Total2 with the content of File11 to File20... etc, etc... Please your help.
Thanx
Lestat,
See if this works for you:
typeset -i mCnt=0
typeset -i mSeq=1
mOutFile='Total1'
for mFName in `find . -type f`
do
cat mFName >> $mOutFile
mCnt=$mCnt+1
if [ ${mCnt} -eq 10 ]; then
mSeq=$mSeq+1
mOutFile='Total'$mSeq
mCnt=0
fi
done
Another solution (ksh or bash) :
# Script file: concat.sh
shopt -s extglob # Not need for ksh
src_prefix=File
out_prefix=Total
typeset -i src_seq out_seq
for src_file in ${src_prefix}+([0-9])
do
src_seq=${src_file#${src_prefix}}
(( out_seq=src_seq/10+1 ))
echo "cat ${src_file} >> ${out_prefix}${out_seq}"
done
Execution:
$ ls File*
File1 File100 File11 File19 File2 File9 File91 File92 File99
$ concat.sh
cat File1 >> Total1
cat File100 >> Total11
cat File11 >> Total2
cat File19 >> Total2
cat File2 >> Total1
cat File9 >> Total1
cat File91 >> Total10
cat File92 >> Total10
cat File99 >> Total10$
Jean-Pierre.
code
#!/bin/sh
echo enter file name
read f
echo enter number of such files
read n
i=1
while test $i -ne $n
do
echo $f$i >> outfile
cat $f$i >> outfile
i=`expr $i + 1`
done
by the use of above code you can see the file name also before its content.