If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Dynamic calculation

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-03-08, 05:32
shary shary is offline
Registered User
 
Join Date: Nov 2008
Posts: 4
Dynamic calculation

Hi Experts,

please see below are the dynamic text files.

abc,445,hhh,data,10,country,data,88,city
abc,445,hhh,data,10,country,data,88,city
abc,445,hhh,data,10,country,data,88,city
abc,445,hhh,data,10,country,data,200,city
abc,445,hhh,data,10,country,data,88,city,games,100 ,data,20,basket
abc,445,hhh,data,10,country,data,88,city,games,100 ,data,20,basket
abc,445,hhh,data,10,country,data,88,city,games,100 ,data,220,basket

I need a shell script which can show me those lines which is having data greater than 200.
for example in the below line
abc,445,hhh,data,10,country,data,88,city
script should add the data value like 10+88=98 and if the value is greater than 200 it should print the whole line like
abc,445,hhh,data,10,country,data,88,city,games,100 ,data,220,basket
data =10+88+220 is greater than 200 so it should print the whole line.

Note the data value is dynamic it can appera many times in the file.

Thanks
Reply With Quote
  #2 (permalink)  
Old 11-03-08, 06:22
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Shary

Please post what you've tried so far.
If this is a course work then please post full question.

Mike
Reply With Quote
  #3 (permalink)  
Old 11-04-08, 00:50
shary shary is offline
Registered User
 
Join Date: Nov 2008
Posts: 4
Dears,

The problem is the text which i have mentioned is dynamic and so far i can not understand how to deal with dynamic fields like data string can appear many times in the file and i want to calculate the next filed after the data string and then sum up all the numbers coming just after the data and compare with 200 and if it is greater than 200 then the script should display me those lines
Reply With Quote
  #4 (permalink)  
Old 11-04-08, 07:18
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
  • Set up a for loop to read each line.
  • Remove all characters from the line that are not numbers but remember to separate the numbers from each other (use sed command).
  • Loop through each number adding them up (look up expr command ie COUNT=`expr $COUNT + 1`).
  • If greater than 200 then print out original line.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On