I am writing a search script for my Unix class. I need to be able to display only user specified fields from a matched search. This is my script:
while echo "Please enter Play name to search: \c" ; do
read string
case $string in
""
tput clear
echo "No entry; please try again \n" ; continue ;;
*) tput clear
grep -i "$string" data > revsearch 2>/dev/null
if [ ! -s revsearch ]
then
echo "This record does not exist" ; exit 1
fi
echo "Play(s) found.\n\n\nPlease select the field(s) that should be displayed from the list below.\n\n Multiple Fields should be separated by a comma (ex. 1,3,5).\n\n1.Play name\n2.Date\n3.Total # of available seats\n4.Number of Seats sold\n5.Price per seat\n6.Total Revenue\n\nEnter field number(s): \c"
read field
tput clear
cat revsearch | awk -f titles2.awk > tempsearch
cut -f"$field" tempsearch > final
echo "Another Search? (y/n): \c"
read anymore
case "$anymore" in
[yY]) tput clear ; continue ;;
*) rm revsearch
rm tempsearch
tput clear
break
esac
done
The AWK script is:
BEGIN {FS = ":" ; printf ("%-20s %-9s %-6s %-8s %-12s %-10s\n\n", "Play Name", "Date", "Seats", "Seats Sold", "Seat Price", "Total Revenue"
}
{printf ("%-20s %8s %6d %-13d $%-12.2f $%-18.2f\n", $1, $2, $3, $4, $5, $6) }
END {print "There are", NR, "records found"}
I am getting the following results when I specify only fields 1,2 to display:
Play Name Date Seats Seats Sold
Hamlet 01/10/03 0 0 $0.00 $0.00
Hamlet 01/10/03 0 0 $0.00 $0.00
There are 2 records found
Another Search? (y/n):
How do I rewrite this so that only specified fields get to my report? My original data file looks like this:
Hamlet:01/10/03:100.5:5:0.00: 0.00
Hamlet:01/10/03:500:5:0.00: 0.00
Thank you for your help.
Natalie.
while echo "Please enter Play name to search: \c" ; do
read string
case $string in
""
echo "No entry; please try again \n" ; continue ;;
*) tput clear
grep -i "$string" data > revsearch 2>/dev/null
if [ ! -s revsearch ]
then
echo "This record does not exist" ; exit 1
fi
echo "Play(s) found.\n\n\nPlease select the field(s) that should be displayed from the list below.\n\n Multiple Fields should be separated by a comma (ex. 1,3,5).\n\n1.Play name\n2.Date\n3.Total # of available seats\n4.Number of Seats sold\n5.Price per seat\n6.Total Revenue\n\nEnter field number(s): \c"
read field
tput clear
cat revsearch | awk -f titles2.awk > tempsearch
cut -f"$field" tempsearch > final
echo "Another Search? (y/n): \c"
read anymore
case "$anymore" in
[yY]) tput clear ; continue ;;
*) rm revsearch
rm tempsearch
tput clear
break
esac
done
The AWK script is:
BEGIN {FS = ":" ; printf ("%-20s %-9s %-6s %-8s %-12s %-10s\n\n", "Play Name", "Date", "Seats", "Seats Sold", "Seat Price", "Total Revenue"
{printf ("%-20s %8s %6d %-13d $%-12.2f $%-18.2f\n", $1, $2, $3, $4, $5, $6) }
END {print "There are", NR, "records found"}
I am getting the following results when I specify only fields 1,2 to display:
Play Name Date Seats Seats Sold
Hamlet 01/10/03 0 0 $0.00 $0.00
Hamlet 01/10/03 0 0 $0.00 $0.00
There are 2 records found
Another Search? (y/n):
How do I rewrite this so that only specified fields get to my report? My original data file looks like this:
Hamlet:01/10/03:100.5:5:0.00: 0.00
Hamlet:01/10/03:500:5:0.00: 0.00
Thank you for your help.
Natalie.