Shell Script To Combine Any Three Text Files Into a Single File

in Categories Academic, File-management last updated March 12, 2014
#!/bin/bash
# Write a shell script to combine any three text files into a single file
# (append them in the order as then appear in the arguments) and display 
# the word count.
# -------------------------------------------------------------------------
# Copyright (c) 2001 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
file1=$1
file2=$2
file3=$3
out="output.$$"
count=0
 
if [ $# -ne 3 ]
then
	echo "$(basename $0) file1 file2 file3"
	exit 1
fi
 
if [ ! -f $file1 ] 
then
	echo "$file1 not a file!"
	exit 2
fi
 
if [ ! -f $file2 ] 
then
	echo "$file2 not a file!"
	exit 3
fi
 
if [ ! -f $file3 ] 
then
	echo "$file3 not a file!"
	exit 4 
fi
 
cat $file1 $file2 $file3 >> $out
count=$(cat $out | wc -w)
echo "$count words written to $out"

Share this on:

3 comment

    Have a question? Post it on our forum!