Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Unix-ksh script

Original Message
Name: Apple
Date: March 8, 2005 at 06:06:56 Pacific
Subject: Unix-ksh script
OS: ?
CPU/Ram: ?
Comment:
Hello All,

The script need to read a file, which contains such as follows:

Mary age22 address1
Peter age33 address2
eof

then all data put into variable

Output like : name = Mary
age = age22
address = address1

name = Peter
age = age33
address = address2


Report Offensive Message For Removal


Response Number 1
Name: David Perry
Date: March 8, 2005 at 07:28:00 Pacific
Subject: Unix-ksh script
Reply: (edit)
awk '$2 = substr($2,4) { printf "name = %s\nage = %s\naddress = %s\n\n", $1, $2, $3 }' infile

Report Offensive Follow Up For Removal

Response Number 2
Name: Jim Boothe
Date: March 8, 2005 at 08:25:18 Pacific
Subject: Unix-ksh script
Reply: (edit)
If you want output formatting, David gave you a solution for that.  If you need to load the fields into variables so that you can work with the variables, the script below provides an example:

while read name age addr other
do
# echo name=$name age=$age addr=$addr
printf "name=%-10s age=%-6s addr=%s\n" $name $age $addr
done < infile


Report Offensive Follow Up For Removal

Response Number 3
Name: Apple
Date: March 13, 2005 at 06:02:45 Pacific
Subject: Unix-ksh script
Reply: (edit)
Thanks.

I have another question.

If I want to get exactly field size of name, age and address. What should I do?

for example: input name = char 5
age = char 2
addr = char 40


while true
$read name | cut -d -f,5
$read age | cut -d -f,2
$read addr | cut -d -f ,40
do
# echo name=$name age=$age addr=$addr
printf "name=%-10s age=%-6s addr=%s\n" $name $age $addr
done < infile

Can I do like that?

Thanks and Regards,
Apple




Report Offensive Follow Up For Removal

Response Number 4
Name: Jim Boothe
Date: March 14, 2005 at 07:37:01 Pacific
Subject: Unix-ksh script
Reply: (edit)
If you are asking how to pull variables from each line of your file based upon fixed column positions, then I believe you have the general idea of it. In the ksh script below, the while-loop reads each line of infile into the $line variable, then I show two different ways to split up $line based upon fixed column positions.

IFS=""

while read line
do

name=$(expr "$line" : "\(.....\)")
age=$(expr  "$line" : ".....\(..\)")
addr=$(expr "$line" : ".......\(.*\)")

#name=$(echo $line | cut -c1-5)
#age=$(echo $line | cut -c6-7)
#addr=$(echo $line | cut -c8-)

printf "name=%-6s age=%-3s addr=%s\n" $name $age $addr
done < infile


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Unix-ksh script

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




XP Installed to G?

exessive internet traffic

ZoneAlarm Question. Blocked Connect

Windows Live Messenger Problem

Delete $Uninstall after SP3 updates


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC