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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using printf to display information.

Status
Not open for further replies.

jalge2

Technical User
Feb 5, 2003
105
US
Hi all. I'll try to lay out the scenario. We have certain battery numbers that append to a log file. They look like this:

LRT231,07/01/03,00:01,9901789,Patricia Welty,Receiving,IN,4130801,PROD
LRT231,07/01/03,00:01,9901789,Patricia Welty,Receiving,OUT,0160503,PROD

I am creating a script that will grep for any battery number that I enter, in this case, 4130801 is the battery number. I'm using printf to display the information but it's not working because of the comma's in between the certain fields in the log file. I'm trying to figure out how to use IFS and OFS but am having some trouble. My script looks like this:

# /bin/ksh!
echo "\tPlease enter the battery number you wish to search for..."
read ans

if [[ $ans != 0 ]]

then
cat /dso/eis/log/batlog.log.* | grep $ans > /dso/eis/log/battsearch.log
awk 'BEGIN { print "LRT DATE TIME OLD BATT NAME DEPT IN/OUT NEW BATT STATUS"
print "--- ---- ---- -------- ---- ---- ------ -------- ------"}
{ printf "%-5s%-5s%-5s%-5s %s\n", $1, $2, $3, $4, $6, $7, $8, $9 }' /dso/eis/log/battsearch.log
else
exit


fi

echo "\tWould you like to print this? (y/n)"
read ans1

if [ $ans1 = "y" -o $ans1 = "Y" ]

then
lp -dps@pr7 /dso/eis/log/battsearch.log

else
exit
fi

The output that I get is...


LRT DATE TIME OLD BATT NAME DEPT IN/OUT NEW BATT STATUS
--- ---- ---- -------- ---- ---- ------ -------- ------
,06/29/03,22:04,9901538,CoryCole,DCO,OUT,4270801,PROD
LRT194,07/01/03,08:45,9901858,ShelleyNoe,Receiving,OUT,4270801,PROD


All one line of course. I'm trying to figure out the field separators but am having trouble. Can someone help me figure out where they need to be in order to get this to output correctly.

Thanks

jalge2
 
Just use awk -F,'BEGIN { print "LR.....etc etc

Dickie Bird (:)-)))
 
You also left out $5 and there has to be a format string for each field. Like...

awk -F, 'BEGIN { print "LRT DATE TIME OLD BATT NAME DEPT IN/OUT NEW BATT STATUS";
print "--- ---- ---- -------- ---- ---- ------ -------- ------"}
{ printf "%-7s %-8s %-8s %-12s %-.8s %-.8s %-10s %-12s %-8s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9 }'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top