Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2008
    Posts
    4

    Unanswered: 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

  2. #2
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    Shary

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

    Mike

  3. #3
    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

  4. #4
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    • 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •