#!/bin/ksh
# Version 1.0 Original 11/08/01 jsmith
function PrintUsage
{
echo ""
echo "USAGE: chopFILE file-name [-s/+s string] -e/+e
string [[-s/+s string] -e/+e string] ... "
echo " chopFILE
-h"
echo
echo
"DESCRIPTION: Takes a text file a
cuts it up into a new file composed from"
echo " parts of the original."
echo
echo "OPTIONS:
"
echo
echo " \"-/+s string\" indicates a string
that starts a section. A -s indicates that the"
echo " file should start AFTER the string (not
inclusive). A +s means that string
should be"
echo " included in the file (inclusive)."
echo
echo " \"-/+e string\" indicates a string
that ends a section. A -e indicates that the"
echo " file should start BEFORE the string (not
inclusive). A +e means that string
should be"
echo " included in the file (inclusive)."
echo
echo "NOTE:
New files are created in your current directory."
echo
echo "EXAMPLE:
chopFILE SampleFile +s TARGET1 -e TARGET2 +s TARGET 3 -e \"TARGET4\"
"
echo
echo " input:
SampleFile.txt"
echo " ::::::::::::::"
echo " ## TARGET1 ##"
echo " lskdfj"
echo " aslkdfj"
echo " askdfja"
echo " ## TARGET2 ##"
echo " sdkfja"
echo " sdkfja"
echo " ## TARGET 3 ##"
echo " sdfkj"
echo " ## TARGET4 ##"
echo
echo " output:
SampleFile.new"
echo " ::::::::::::::"
echo " ## TARGET1 ##"
echo " lskdfj"
echo " aslkdfj"
echo " askdfja"
echo " ## TARGET 3 ##"
echo " sdfkj"
echo
exit 1
}
if [[ $1 = "-h" || $1 = "" ]]; then
PrintUsage | more; exit 1; fi
Error=0;
FILE=$1; shift
if [[ ! -a $FILE ]]; then
echo
echo
"!!!!!!!!! $FILE could not be found !!!!!!!!!!"
echo
echo "Exiting
chopFILE"
echo
exit 1
fi
fileNAME=`basename $FILE`
localDIR=`pwd`"/"
tmpFILE=$localDIR$fileNAME.tmp; cp $FILE $tmpFILE
newFILE=$localDIR$fileNAME.new
Part1=$localDIR$fileNAME.part1
Part2=$localDIR$fileNAME.part2
if (( Error != 0 )); then
echo "\\n!!!!
Could not create $newFILE !!!!\\n"
echo "!!!!
Error Code ($Error) !!!!"
exit 2
fi
while [[ $1 != "" ]]; do
if [[ $1 =
"-s" ]]; then
StartingLine="$2"; shift; shift;
let s1=`grep -n
"$StartingLine" $tmpFILE | head -1 | cut -d":" -f1`+1
let s2=$s1+1
elif [[ $1 =
"+s" ]]; then
StartingLine="$2"; shift; shift;
let s1=`grep -n
"$StartingLine" $tmpFILE | head -1 | cut -d":" -f1`
let s2=$s1+1
else
let s1=0
fi
if (( $s1 != 0 ));
then
tail +$s1
$tmpFILE > $tmpFILE.tmp
mv -f
$tmpFILE.tmp $tmpFILE
fi
if [[ $1 =
"-e" ]]; then
EndingLine="$2"; shift; shift;
let e1=`grep -n
"$EndingLine" $tmpFILE | head -1 | cut -d":" -f1`-1
let e2=$e1+1
elif [[ $1 =
"+e" ]]; then
EndingLine="$2"; shift; shift;
let e1=`grep -n
"$EndingLine" $tmpFILE | head -1 | cut -d":" -f1`
let e2=$e1+1
else
let e1=0
fi
if (( $e1 == 0 ));
then
cat $tmpFILE
>> $newFILE
else
head -$e1
$tmpFILE > $Part1
tail +$e2
$tmpFILE > $Part2
cat $Part1
>> $newFILE
mv -f $Part2 $tmpFILE
fi
done
rm -f $tmpFILE 2>/dev/null
rm -f $Part1 2>/dev/null
rm -f $Part2 2>/dev/null
echo "Finished ... New File $newFILE created. \\n"
exit $Error