I frankly love awk, never really used sed.
Rather than answer the question directly, I'll give you an example script
that's pretty well commented. It does some of the same things you need to
do. I couldn't use perl because it wasn't installed everywhere this script
had to run. :)
It doesn't just use awk, it uses a lot of stuff.
Hint: you want to see the part where it's grepping in the file and use
something like that. :)
#!/bin/sh
SCRIPTLOG=scriptlog;
# sample crontab line to run this Mon-Sat at 5am (replace <scriptname>
# with the final name for the script) -- take out initial # of course.
# use crontab -e to edit the crontab for a user.
# 0 5 * * 1-6 <scriptname>
# make sure and use chmod to make this script executable.
# takes two parameters:
# 1) the input file, e.g. /home/me/foo
# 2) where the output should be written (as a directory with a trailing/)
if [ $(($#)) -lt 2 ]
then
echo "usage: programname source/file/fullpath destination/directory/";
exit;
elif [ $(($#)) -gt 2 ]
then
echo "usage: programname source/file/fullpath destination/directory";
exit;
fi;
# we managed to get exactly two parameters.
echo "\nLog for session: " >> $SCRIPTLOG;
date >> $SCRIPTLOG;
echo "" >> $SCRIPTLOG;
# let's check to see if the file is < 211 characters
FILELENGTH=`wc -c $1 | awk '{ print $1}'`;
echo $FILELENGTH; # for debugging
if [ "0$FILELENGTH" -lt 211 ] # file is too short, exit
then
echo "File is too short, exiting" >> $SCRIPTLOG;
echo "File is too short, exiting";
echo >> $SCRIPTLOG;
exit;
fi
# it's not too short, now see if it has the magic string on the first line
FILECONTENT=`head -n 1 $1 | grep "NO RECORDS"`;
echo $FILECONTENT; # for debugging
if [ "X$FILECONTENT" = "X" ] # X is workaround for bug in null compares
then
: # special case for "ignore this"
else
echo "File has no records, exiting" >> $SCRIPTLOG;
echo "File has no records, exiting";
echo >> $SCRIPTLOG;
exit;
fi
# we've gotten here, thus we have files to write.
# concept:
# 1) write *entire* file to one backup file.
# 2) ftp file with original name to a windows box.
TEMPDATE=`date '+%m%d%Y'`;
# backup entire file
echo $1 $2 $TEMPDATE | awk '{print("cp "$1" "$2"bu_"$3".txt")}' | /bin/sh;
# print the wordcount of original and full backup to the log
echo "Wordcounts: (lines) (words) (characters)" >> $SCRIPTLOG;
echo $1 | awk '{print("wc "$1)}' | /bin/sh >> $SCRIPTLOG;
# wrap it up
echo "Script completed successfully" >> $SCRIPTLOG;
echo "" >> $SCRIPTLOG # cheesy way of putting in another blank line.
# you're done, why are you reading this? :)
_Deirdre * http://disclaimer.deirdre.org * http://www.deirdre.net
"Linux is a very complete and sophisticated operating system," said
[Paul] Maritz, Microsoft's group vice president for platforms and
applications. "There are and will be large numbers of applications
available for it."