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

by Vivek Gite [Last updated: September 17, 2008]

in Academic, File-management

#!/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 $file ]
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"
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our email newsletter to make sure you don't miss a single tip/tricks.

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , ,

Previous post: Shell Script To Translate All Characters In a File To Lowercase

Next post: Shell Script To Write Odd and Even Line To Respective Files