It is currently Wed Jun 28, 2017 12:54 am

combine multiple files into one (with divides)

All times are UTC - 6 hours

Post new topic Reply to topic  [ 1 post ] 
Author Message
 PostPosted: Tue May 17, 2005 7:40 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
originally posted here at

this script takes as many files as you can give it and combines them all to one file
# Author: Joshua Bailey
# Script: divApp2
# This script takes the first argument you entered
# and writes to a file with that name.
# If it exists it prompts to append, overwrite, or exit
# It then opens the other file names you pass it to redirect
# Their output for the first files input.
# syntax: divApp2 fileAddTo addFrom1 addFrom2 addFrom3 ....

# function printBeginning
printBeginning ()
   # append a line break
   echo "================================================================" >> $1

   # append begin
   echo "               <*** Begin \"$2\" ***>" >> $1

   # space holder
   echo " " >> $1
} # end function printBeginning

# function printEnding
printEnding ()
   # space holder
      echo " " >> $1

      # append the end of file line break
      echo "               <*** End \"$2\" ***>" >> $1
      echo "================================================================" >> $1

      # space holder
                echo " " >> $1
      echo " " >> $1
} # end function printEnding

# function argumentCheck
argumentCheck ()
   # check to see if user used enough arguments
   if [[ ! $1 > 1 ]]
      # tell the user that the syntax was entered incorrectly
      echo "*** Error: too few arguments!"
      echo "*** Must have a file to append to or create."
      echo "*** Must also have atleast one file to read from."
      echo "*** Syntax: divApp2 fileAddTo addFrom1 addFrom2 addFrom3..."
} # end function argumentCheck

# function existFileAddTo
existFileAddTo ()
   if [[ -e $1 ]]

      # if it exists say so
      echo "File \"$1\" already exists."

      # ask user what they want to do
      read -p "Do you want to append, overwrite, or exit? (a|o|e) " ans

      # make sure the variable holds the correct case
      ans=`echo $ans | tr [:upper:] [:lower:]`

      # use only the first letter to decide what to do

      # do what the user requested
      if [[ $ans = "e" ]]
         echo "*** Exiting ***"
      elif [[ $ans = "o" ]]
         echo "*** Overwriting ***"
         rm -f $1
      elif [[ $ans = "a" ]]
         echo "*** Appending ***"
         if [[ $ans != "T" ]]
            echo "That choice is not an option!"
            echo "*** Exiting ***"
} # end function existFileAddTo

# function viewFile
viewFile ()
   read -p "Do you want to display the file? (y|n) " YesNo

   # make user input lower case
   YesNo=`echo $YesNo | tr [:upper:] [:lower:]`

   # cut off all except the first letter

   if [[ $YesNo = "y" ]]

      # display the file
      cat $1 | less
} # end function viewFile

# assign the first parameter (the created file) to copyTo

# assign the number of parameters to loopCount

# declare the array
declare -a arr

# check to see if user used enough arguments
argumentCheck $loopCount

# assign the arguments to an array
for ((i=0; i != ($loopCount-1); i++))
   shift 1

   # see if all files listed exist...exit if they don't
   if [[ ! -e ${arr[$i]} ]]
      echo "File \"${arr[$i]}\" does not exist, or you typed the wrong path!"
      echo "*** Aborting ***"

   # see if the fileAddTo file exists or not
   existFileAddTo $copyTo

   # loop the appending the number of parameters minus one (the first one)
   for ((i=0; i < `echo ${#arr[*]}`; i++))

      printBeginning $copyTo ${arr[$i]}
      # append the contents of the file you specified
      cat ${arr[$i]} >> $copyTo

      printEnding $copyTo ${arr[$i]}


   # state that the process is complete
   echo "*** Done ***"

   # see if user wants to view the file created
   viewFile $copyTo

this script was written with log files in mind...i think it's good for taking multiple log files that have to be looked at on a semi-daily can edit it to include date stamps and whatnot on the dividing sections (printBeginning and printEnding) which would help with organizing...this would be a good cron candidate for making reports using different log files.
like setting cron to run the script as
divApp2 logfiletomake.$todaytag logfile1 logfile2 logfile3...

doing that would take the log files on a regular basis and appending them all to a file with the current date string (i would shorten it personally by editing the string with different tags for the date app)
it also makes printing the different logs easy (saves paper too Smile )

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: Google [Bot] and 11 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group