Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
How do you print an entire column from a seperate file. I need to write a script entirely in korn that will prompt the user to enter the fields that they wish to display, and with that information display the correct fields. The data file looks similiar to this.
filename: inventorycolor size item
red small shirt
blue large skirt
green medium hatSay a user ask for columns 1&3 (color column and the item column). The field should print:
color item
red shirt
blue skirt
green hat

#! /bin/ksh
# Print columns specified by the user
FILE=/path/to/fileprint "Enter the first column to print \c "
read COL1print "Enter the second column to print \c "
read COL2nawk "{printf \"%-10s %10s\n\", \$$COL1, \$$COL2}" $FILE

Hi:
Jimbo's solution is good, but if you really need to eliminate the (n)awk call, you can use the shell:
#!/bin/ksh
print "Enter the first column to print \c "
read COL1print "Enter the second column to print \c "
read COL2# build the echo command string
bcom="echo \$$COL1 \$$COL2"
echo $bcom
while read line
do
set - `echo $line`
eval $bcom
done data.file
# the above line a less than sign between the done and data.fileI build a string to determine which arguments to echo, 1,2, or 3. The while loop reads each line. the set command sets the arguments in the shell $1, $2, $3. Finally, I force an execution of the built command producing the results.
Regards,
Nails

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |