Register
It is currently Thu Sep 18, 2014 11:40 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 usalug.org

this script takes as many files as you can give it and combines them all to one file
Code:
#!/bin/bash
# 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 ]]
   then
      # 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..."
      exit
   fi
} # end function argumentCheck

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

      # 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
      ans=${ans:0:1}

      # do what the user requested
      if [[ $ans = "e" ]]
      then
         echo "*** Exiting ***"
         exit
      elif [[ $ans = "o" ]]
      then
         echo "*** Overwriting ***"
         rm -f $1
         ans="T"
      elif [[ $ans = "a" ]]
      then
         echo "*** Appending ***"
         ans="T"
      else
         if [[ $ans != "T" ]]
         then
            echo "That choice is not an option!"
            echo "*** Exiting ***"
            exit
         fi
      fi
   fi
} # 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
   YesNo=${YesNo:0:1}

   if [[ $YesNo = "y" ]]
   then

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

# assign the first parameter (the created file) to copyTo
copyTo=$1

# assign the number of parameters to loopCount
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++))
do
   arr[$i]=$2
   shift 1

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

   # 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++))
   do

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

      printEnding $copyTo ${arr[$i]}

   done

   # 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 basis...you 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
Code:
todaytag=`date`
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 )


Top
 Profile  
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: Bing [Bot] and 4 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
© 2003 - 2011 USA LINUX USERS GROUP