Today i will discuss about the shell script which will merge the multiple files into one fileĀ  and post merging , archive the source files. Below is the code for the same.

  1. Here, i am passing four argumentsĀ  namely source file location,Archive file location,merged file name pattern,source file name pattern.
  2. Using date_1 variable which convertsĀ  the date into format YYYYMMDD_HHMMSS.
  3. Using “cd” command to go source file location.
  4. Count number of files in that location.
  5. If count is equal to 1 , that means there is no need to merge.
  6. In Else part, using “for loop” to hold list of files using source file pattern and awk command.
  7. “print $9” -> will give us only filenames.
  8. Then using cat command , merging the source file one by one into merged file.
  9. At the end, archiving the source files.
  10. Finally , closing “if ” block.

date_1=`date +%Y%m%d_%H%M%S`
source_file_loc=$1;
archive_file_loc=$2;
destination_file_patrn=$3;
source_file_patrn=$4;

cd $source_file_location

cnt=`ls -lrt $source_file_location| wc -l`
if [ $cnt == 1 ]; then
echo “No of input files is one. No Action Required.”
else

for list in `ls -lrt $source_file_location| grep -i $source_file_pattern| awk ‘{print $9}’`
do

echo $list

cat $list >> $source_file_location$destination_file_pattern”_”$date_1.csv
mv $list $archive_file_location;
done
fi

Leave a Reply

Your email address will not be published. Required fields are marked *