Operating System - HP-UX
1830939 Members
2764 Online
110017 Solutions
New Discussion

Re: calculations in a korn script

 
SOLVED
Go to solution

calculations in a korn script

i'm gonna start hacking away here, but if someone can help me cut to the chase, that would be great. i have to put a script together that can split a file into two halves. for example, if a file contains 127 records, i'd like to run the head and tail commands against the file: head 64 file1 >/tmp/foo ... tail 63 file1 >/tmp/foo1. can i generate the values 64 and 63 perhaps using the "wc" utility and then doing some division and subtraction? can calculations like this be done in a korn script?

thank you all in advance. i know you'll come through again:-)
3 REPLIES 3
RikTytgat
Honored Contributor
Solution

Re: calculations in a korn script

Hi,

Yes, you can do calculations in ksh.

-----
lines=$(wc -l /your/file)
(( half = lines / 2 ))
echo $half
-----

Only integer calculations are possible.

If you want to split a file into pieces, you should check out the split(1) command.

Hope this helps,
Rik.

Re: calculations in a korn script

rik,

good job. that's just what i needed!! THANKS FOR YOUR HELP:-)
James R. Ferguson
Acclaimed Contributor

Re: calculations in a korn script

Hi:

While the shell only allows integer arithmetic, real arithmetic can be done with the 'bc' or 'awk' utilities. See this thread for a recent series of examples.

http://forums.itrc.hp.com/cm/QuestionAnswer/1,1150,0x6bc679bffde7d4118fef0090279cd0f9,00.html

...JRF...