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