Computing.Net > Forums > Unix > Unix-ksh script

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.

Unix-ksh script

Reply to Message Icon

Name: Apple
Date: March 8, 2005 at 06:06:56 Pacific
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



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: March 8, 2005 at 07:28:00 Pacific
Reply:

awk '$2 = substr($2,4) { printf "name = %s\nage = %s\naddress = %s\n\n", $1, $2, $3 }' infile


0

Response Number 2
Name: Jim Boothe
Date: March 8, 2005 at 08:25:18 Pacific
Reply:

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


0

Response Number 3
Name: Apple
Date: March 13, 2005 at 06:02:45 Pacific
Reply:

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




0

Response Number 4
Name: Jim Boothe
Date: March 14, 2005 at 07:37:01 Pacific
Reply:

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


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: Unix-ksh script

Unix shell script for opening a file and www.computing.net/answers/unix/unix-shell-script-for-opening-a-file-and/3452.html

Need Help With KSH Script www.computing.net/answers/unix/need-help-with-ksh-script/6747.html

ksh script www.computing.net/answers/unix/ksh-script/6748.html