Find the answer to your Linux question:
Results 1 to 6 of 6
Hi All, I need to calculate time period spent. The Start and End time are available in a file: #cat trna.3000_3010.time Tue Dec 25 11:10:25 IST 2007 Tue Dec 25 ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    13

    script to calculate time spent

    Hi All,

    I need to calculate time period spent. The Start and End time are available in a file:

    #cat trna.3000_3010.time
    Tue Dec 25 11:10:25 IST 2007
    Tue Dec 25 12:40:07 IST 2007

    I did the following to get "seconds" numbers as follows.
    # cat trna.3000_3010.time | cut -d " " -f 4 | cut -d : -f 3
    25
    07

    Now I'm not getting, how to get these numbers(25, 07) individually and to store these in a variable.


    Can any one tell how to do this?

    - Sangamesh

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by sangamesh.bc View Post
    Hi All,

    I need to calculate time period spent. The Start and End time are available in a file:

    #cat trna.3000_3010.time
    Tue Dec 25 11:10:25 IST 2007
    Tue Dec 25 12:40:07 IST 2007

    I did the following to get "seconds" numbers as follows.
    # cat trna.3000_3010.time | cut -d " " -f 4 | cut -d : -f 3
    25
    07

    Now I'm not getting, how to get these numbers(25, 07) individually and to store these in a variable.


    Can any one tell how to do this?

    - Sangamesh
    Code:
    cat file | cut -d " " -f 4 | cut -d : -f 3 | while read num
    do 
     echo "do something with $num"
    done

  3. #3
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by ghostdog74 View Post
    Code:
    cat file | cut -d " " -f 4 | cut -d : -f 3 | while read num
    do 
     echo "do something with $num"
    done
    Instead of using cat and cut 2 times you can use sed:

    Code:
    sed 's/.*:\(..\) .*/\1/' file | while read num
    do 
     echo "do something with $num"
    done
    Regards

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by Franklin52 View Post
    Instead of using cat and cut 2 times you can use sed:

    Code:
    sed 's/.*:\(..\) .*/\1/' file | while read num
    do 
     echo "do something with $num"
    done
    Regards
    I hope OP gets the point. I wouldn't have done it like what OP did, or even with sed. cheers

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by ghostdog74 View Post
    I hope OP gets the point. I wouldn't have done it like what OP did, or even with sed. cheers
    I know, something like "echo $(num:17:2)" is sufficient, it's not a offence to you but a point for the OP.

    Regards

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by Franklin52 View Post
    I know, something like "echo $(num:17:2)" is sufficient, it's not a offence to you but a point for the OP.

    Regards
    no worries noted. I would use awk for myself if I have this problem, however I would rather show bash and its useful features to OP without the use of cut/sed/awk (for this purpose), just for OP to get the feel of bash.
    Code:
    while read -a line
    do
      echo ${line[3]##*:}
    done < "file"

Posting Permissions

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