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

Need help on using "Echo" Command (or AWK) for alignment

Status
Not open for further replies.

Tamrak

MIS
Jan 18, 2001
213
US
Hello,

I use Bash shell and I will be very much appreciated all of your help.

I have a lot of records in the flat file, called "Employees." Let's say a sample of three records ->

1001:JOHNSON:ROBERT J:90:70:85:80
1002:ROBERTS:JANE C:80:82:87:88
1004:VILLANUEVA:JEANNETTE K:90:92:94:92

Let's say that the last 4 fields are the test score and I have already completed all necessary computation. The average score of Johnson will be 81, which is equivalent to B. Roberts has an average of 84, which will be equivalent to B and Villanueva has an average of 92, which is equivalent to A.

The variable names for average is "saverage" and the letter grade is "ltr".

I have already completed the program that will print employee ID, Name, Last Name, saverage and ltr. The variables "saverage" and "ltr" are not in the employee file.

When I use the echo command -> echo "$id $fname $lname $saverage $ltr", I got the print out. However, the alignment looks terrible. I got something like this:

SN# Name Last Name Average Ltr
1001 ROBERT JOHNSON 81 B
1002 JANE ROBERTS 84 B
1003 JEANNETTE VILLANUEVA 92 A

Look like the spacing is automatic after running the echo command.

My question is very simple. How can I align the print out to correct my current spacing issue? The awk command might be the best but I do not think I can use the syntax like this:

awk -F: '{printf "%-5s %-12s %s %d %s\n",$1, $3, $2, $saverage, $ltr}' employees.

If some commands like this work, I know that I can solve the alignment problem. Since there are no field "average" and "letter grade" in the employees file.

All suggestions are welcome. I do not know much about the Unix command. I will be very much appreciated all of your help. Thank you again.


 
You can use the following for echo:

echo "SSN:\tName:" # This will print SSN, tab, and then print the Name. Although this doesn't always line everything up perfectly due to extended names and what not.

Another alternative is to use the printf command, should be in /usr/bin or /usr/local/bin. That should solve your spacing issues.

For more info on either echo or printf, man echo or man printf.
 
Pmcmicha,

Thank you for suggestion. However, I really would like to align the spacing. Any other suggestions? Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top