1833247 Members
3065 Online
110051 Solutions
New Discussion

Re: while script

 
file system
Frequent Advisor

while script

I want to get the calcuated value each line.

131648
131648
131648
107640
98880
98880
98880
82496
82496
82496
82496
82496
82240
82240
82112
74304
74304
74304
74304
74048
74048
74048

Above lines should be mutiplied by 4096 each.
I want to get calculated value.
which script is it fit for?

I want to thank you in advance.
4 REPLIES 4
Sunil Sharma_1
Honored Contributor

Re: while script

Hi,

You can try simple for loop for this. Store this list in a file say a.txt and then run following command

for i in $(cat a.txt)
do
bc $i*4096 >>/tmp/a.out
done


You will get output in /tmp/a.out

Sunil
*** Dream as if you'll live forever. Live as if you'll die today ***
Ivan Krastev
Honored Contributor

Re: while script

Try this one:


#!/usr/bin/ksh
for i in `cat test.txt`
do
ii=$(( $i / 4096 ));
print $ii;
done



regards,
ivan
Peter Godron
Honored Contributor

Re: while script

HI,
as you asked for a while statement:

#!/usr/bin/sh
while read a
do
echo `expr $a \* 4096`
done < a.lis

The script reads one line at a time from a.lis and multiplies the read amount by 4096.

The "\" before the multiplication sign is to make it clear to the shell that you want to use the "*" as a multiplication sign.

You can amend the echo statement to re-direct output and/or display the read value.
Hein van den Heuvel
Honored Contributor

Re: while script

Of course it may be more convenient to use awk or perl:

awk '{print 4096*$1}' old.txt > new.txt

perl -ple '$_ *= 4096' old.txt > new.txt


fwiw,
Hein.