Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with report format

Status
Not open for further replies.

nnikolay

Technical User
Sep 27, 2003
5
AU
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.
 
cut -d' ' -f"$field" tempsearch > final


Hope This Help
PH.
 
Thank you, but this is what I am getting after the change:

Play Name

Hamlet
Hamlet
There are
Another Search? (y/n):

Natalie.
 
Natalie,

Is the script you posted missing an "esac" that corresponds with the first case?

Thanks,

John
 
I used the script below and added an "esac":

#!/usr/bin/ksh

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 shouldbe displayed from the list below.\n\n Multiple Fields should be separatedby 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 fieldnumber(s): \c"
esac
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
and it seems to work fine.

I entered in Hamlet and selected 1,2 and below is the output I received.

Play Name Date Seats Seats Sold Seat Price Total Revenue

Hamlet 01/10/03 100 5 $0.00 $0.00

Hamlet 01/10/03 500 5 $0.00 $0.00

There are 2 records found

Thanks,

John
[afro]
 
Oops...I misunderstood the problem. Please disregard the post above.

John
 
OK, I think I have it now...

#!/usr/bin/ksh

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 shouldbe displayed from the list below.\n\n Multiple Fields should be separatedby 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 fieldnumber(s): \c"
esac
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

Then change your awk file to be (tab delimited):

BEGIN {FS = ":" ; printf ("%-20s\t%-9s\t%-6s\t%-8s\t%-2s\t%-10s\n\n", "Play Name","Date", "Seats", "Seats Sold", "Seat Price", "Total Revenue") }{printf ("%-20s\t%8s\t%6d\t%-13d\t$%-12.2f\t$%-18.2f\n", $1, $2, $3, $4, $5, $6)} END {print "There are", NR, "records found"}

And that should do the trick.

Thanks,

John
 
Great, it works now!! Thank you very much for your help!!!
Natalie.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top