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

by on September 17, 2008 · 1 comment

  1. #!/bin/bash
  2. # Write a shell script to combine any three text files into a single file
  3. # (append them in the order as then appear in the arguments) and display
  4. # the word count.
  5. # -------------------------------------------------------------------------
  6. # Copyright (c) 2001 nixCraft project <http://cyberciti.biz/fb/>
  7. # This script is licensed under GNU GPL version 2.0 or above
  8. # -------------------------------------------------------------------------
  9. # This script is part of nixCraft shell script collection (NSSC)
  10. # Visit http://bash.cyberciti.biz/ for more information.
  11. # -------------------------------------------------------------------------
  12. file1=$1
  13. file2=$2
  14. file3=$3
  15. out="output.$$"
  16. count=0
  17.  
  18. if [ $# -ne 3 ]
  19. then
  20. echo "$(basename $0) file1 file2 file3"
  21. exit 1
  22. fi
  23.  
  24. if [ ! -f $file1 ]
  25. then
  26. echo "$file1 not a file!"
  27. exit 2
  28. fi
  29.  
  30. if [ ! -f $file2 ]
  31. then
  32. echo "$file2 not a file!"
  33. exit 3
  34. fi
  35.  
  36. if [ ! -f $file3 ]
  37. then
  38. echo "$file3 not a file!"
  39. exit 4
  40. fi
  41.  
  42. cat $file1 $file2 $file3 >> $out
  43. count=$(cat $out | wc -w)
  44. echo "$count words written to $out"


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • hunterm

    There’s also this really nice command called:
    $ cat file1 file2 file3 >combine

Previous Script:

Next Script: