Computing.Net > Forums > Unix > How to print columns from file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to print columns from file

Reply to Message Icon

Name: WANDr
Date: March 25, 2003 at 10:49:14 Pacific
OS: Unix
CPU/Ram: AMD 512
Comment:

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: inventory

color size item
red small shirt
blue large skirt
green medium hat

Say 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




Sponsored Link
Ads by Google

Response Number 1
Name: Jimbo
Date: March 25, 2003 at 23:21:00 Pacific
Reply:

#! /bin/ksh
# Print columns specified by the user
FILE=/path/to/file

print "Enter the first column to print \c "
read COL1

print "Enter the second column to print \c "
read COL2

nawk "{printf \"%-10s %10s\n\", \$$COL1, \$$COL2}" $FILE


0

Response Number 2
Name: nails
Date: March 26, 2003 at 09:23:51 Pacific
Reply:

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 COL1

print "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.file

I 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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: How to print columns from file

print columns from files to a file www.computing.net/answers/unix/print-columns-from-files-to-a-file/7615.html

How to Remove LILO from Windows NT boot www.computing.net/answers/unix/how-to-remove-lilo-from-windows-nt-boot-/2230.html

How to install drivers from CD rom www.computing.net/answers/unix/how-to-install-drivers-from-cd-rom/1716.html