Program to calculate the operation from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Program to calculate the operation from file
# 1  
Old 12-22-2011
Program to calculate the operation from file

Hi I have

the file has some formulas how to create a program (per, ksh, awk) which calculate the operation and put the result on other file

example
data of source file - this has separate by ";"
Code:
flashpool;50.8*1024*1024*1024/1024;178*1024*1024*1024/1024;15/1024;49/1024;128*1024/1024;1.98*1024*1024/1024;

I want to out as
Code:
flashpool;53267660.8;186646528.0;0.015;0.048;128.0;2027.5

50.8*1024*1024*1024/1024= 53267660.8
178*1024*1024*1024/1024=86646528.0
5/1024=0.015

I trying :
Code:
   FILENAME=file.tmp
  count=0
  >$FILENAME.out
  cat $FILENAME |awk -F";" '{print $1" " $2" "$3 " "$4" "$5" " $6" "$7 " "$8}'| while read cell pool alloc free oread owrite bread bwrite
  do
     v_alloc=`echo $alloc|bc -l`
     v_free=`echo $free|bc -l`
     v_oread=`echo $oread|bc -l`
     v_owrite=`echo $owrite|bc -l`
     v_bread=`echo $bread|bc -l`
     v_bwrite=`echo $bwrite|bc -l`
    if [ $count -eq 0 ]
    then
        echo $cell >>$FILENAME.out
        (( count = count + 1 ))
    else
        echo $pool"|"$v_alloc "|" $v_free "|" $v_oread "|" $v_owrite "|" $v_bread "|" $v_bwrite  >>$FILENAME.out
        (( count = count + 1 ))
    fi
  done

this work but is very very slow.

someone could help me to run more faster?
any advice .

Miguel

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 12-23-2011 at 03:57 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 12-23-2011
Try this...
Code:
awk -F";" '{ for(i=2;i<=NF;i++){"echo \"scale=6;"$i"\"|bc"|getline x; print x} }' input_file

--ahamed
# 3  
Old 12-23-2011
I am getting the error
awk: syntax error near line 1
awk: illegal statement near line 1
# 4  
Old 12-23-2011
If Solaris, use nawk

--ahamed
# 5  
Old 12-23-2011
I got the error

nawk: echo "scale=2;5/1024"|bc makes too many open files
# 6  
Old 12-23-2011
Try this...
Code:
#!/bin/bash

for i in `nawk -F";" '{for(i=2;i<=NF;i++){printf $i OFS}}' input_file`
do
  echo "scale=6;$i" | bc
done

--ahamed
This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need help in not fetching a file while file writing operation is not completed

Hi All, We have a Unix program in oracle when we run the program this connects to specified ftp and will get the file into local server. We are facing a problem like when file writing operations is not completed, this program is getting the incomplete file. Could anyone please help me... (2 Replies)
Discussion started by: world.apps
2 Replies

2. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

3. Shell Programming and Scripting

File Operation when delimiter is (Ctrl+A)

Hi, I am new to Shell Scripting. I need the following help. I have a file which is Ctrl +A delimited.I have to do the following validation 1. Header row second column should be having the SYSdate time stamp.(ddmmyyyyhhmiss) Need compare only sysdate. Time is not required. 2. ln Footer... (2 Replies)
Discussion started by: gatha_vdm
2 Replies

4. Emergency UNIX and Linux Support

Logic for file copy operation

Hi, i need to copy contents from source to destination with a few conditions, Please helpme out. Sample input file $>cat testfile.txt /a/b/c/d | /e/f/g/d (d can be either a file or directory) my conditions are: check if destination is valid and if its a file or directory if its a... (5 Replies)
Discussion started by: raghu_shekar
5 Replies

5. Shell Programming and Scripting

Column operation : cosne and sine operation

I have a txt file with several columns and i want to peform an operation on two columns and output it to a new txt file . file.txt 900.00000 1 1 1 500.00000 500.00000 100000.000 4 4 1.45257346E-07 899.10834 ... (4 Replies)
Discussion started by: shashi792
4 Replies

6. Linux

File read/ write operation

Hi, I am creating a progress bar for file upload for which I have CGI script which copies the data and depending on certain bytes it increments the progress bar. Here, I am writing the incremented value to a file which is read by Ajax at server end. However, here I want to ask that, is it... (18 Replies)
Discussion started by: xs2punit
18 Replies

7. Shell Programming and Scripting

File Operation

I have one text file like 1 filename 2 filename2 3 hi 4 myname i have one variable in which i have index value..i.e.1,2,3 and so... i want to copy value after the index into somevariable..... how i can do it.... (2 Replies)
Discussion started by: AbhijitIT
2 Replies

8. UNIX for Dummies Questions & Answers

Script producing error, Program to calculate maximum number

Hi folks, Here i have written a shell script to calculate a maximum number from 10 numbers entered on command line. max=0 echo Enter 10 numbers , one at a time for i in 1 2 3 4 5 6 7 8 9 10 do read n max=`expr $max + $n` if --- At this last step there is some problem, it gives error... (5 Replies)
Discussion started by: rits
5 Replies

9. Shell Programming and Scripting

program to calculate distance between 5 atoms

Hello, I am a beginner with perl. I have a perl program to calculate the distance between 5 atoms or more. i have an array which looks like this: 6.324 32.707 50.379 5.197 32.618 46.826 4.020 36.132 46.259 7.131 38.210 45.919 6.719 38.935 42.270 2.986 39.221 ... (1 Reply)
Discussion started by: annie_singh
1 Replies

10. Shell Programming and Scripting

line operation in a file

Hello all, I have just started fidling with script. Can anyone please help me out with this question. I have a file that contains the below 4 lines of words Abcd Efgh Ijkl Mnop The question is I want to add a line to the middle of these without using sed or awk. Can anyone bail me out... (4 Replies)
Discussion started by: Musaroro
4 Replies
Login or Register to Ask a Question