Announcement

Collapse
No announcement yet.

script for fetching data from a file

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    script for fetching data from a file

    Dear friends

    I have a file which is something like this

    21/Feb/2013:15:25:54 changelogs.ubuntu.com
    21/Feb/2013:15:27:48 ocsp.verisign.com
    21/Feb/2013:15:27:48 ocsp.verisign.com
    21/Feb/2013:15:27:49 ocsp.verisign.com
    21/Feb/2013:15:27:49 ocsp.verisign.com
    21/Feb/2013:15:27:54 www.google.co.in
    21/Feb/2013:15:27:56 translate.google.co.in
    21/Feb/2013:15:27:56 humanlinux.blogspot.in
    21/Feb/2013:15:27:56 s0.cyberciti.org
    21/Feb/2013:15:27:56 assets.hostedtube.com
    21/Feb/2013:15:27:56 www.google.co.in
    21/Feb/2013:15:27:56 www.google.co.in
    21/Feb/2013:15:27:56 www.google.co.in
    21/Feb/2013:15:27:57 www.google.co.in
    22/Feb/2013:15:31:50 pagead2.googlesyndication.com
    22/Feb/2013:15:31:51 images.hostedtube.com
    22/Feb/2013:15:31:51 images.hostedtube.com
    22/Feb/2013:15:31:53 images.hostedtube.com
    22/Feb/2013:15:31:55 images.hostedtube.com
    22/Feb/2013:15:31:56 images.hostedtube.com
    22/Feb/2013:15:31:56 images.hostedtube.com




    Now I want to make a script which ask from use a starting date and a ending date like say starting date is 21/Feb/2013 and ending date is 25/feb/2013
    after geting data from user script should fetch data from the file and display on screen.

    if it normal then ok if that data matches the line then it can be but the problem is this that script should match that if user give the date which is not available in the file
    then script should use the very next date and if next date is not available then script should use next to next date and so on..............

    and same if the last date is not available then same script should use just previous date.\


    can I make this in bash then please help me

    thanks ....
    Last edited by farjibaba; Feb 21, 2013, 04:45 AM.

    #2
    Originally posted by farjibaba View Post
    ... make this in bash...
    (Well, bash can do this stuff, but in the not very long run, I'd suggest learning perl or python.)

    The fourth hit in google for "date handling in bash" gave me this function:
    Code:
    date2stamp () {
        date --utc --date "$1" +%s
    }
    Unfortunately, date doesn't grok your dates, so a little fix up is needed:
    Code:
    date2stamp () {
        d1=${1//\// } # remove slashes
        d2=${d1/:/ } # remove first colon
        date --utc --date "$d2" +%s
    }
    This gets the dates into a form that can be compared. Assuming your user can enter the dates and the file on the command line:
    Code:
    (( $# < 3 )) && echo Usage: ${0##*/} start-date end-date file && exit  44
    
    start=$(date2stamp $1) || exit 45
      end=$(date2stamp $2) || exit 46
    
    while read date data; do
        date_utc=$(date2stamp $date)
        (( date_utc >= start && date_utc <= end )) && echo $date $data
    done < $3
    (44, 45, 46 are arbitrary).

    Regards, John Little
    Regards, John Little

    Comment


      #3
      I started to feel, well, if not guilty, perhaps disappointed, in presenting such an inefficient solution as an example to a learner. The techniques used are typical for bash scripts but invoking a program (date) for every line of input is always going to be slow, as is invoking a bash function using $(). So here's a pure bash approach.
      Code:
      #!/bin/bash
      declare -A mn=([Jan]=01
                     [Feb]=02
                     [Mar]=03
                     [Apr]=04
                     [May]=05
                     [Jun]=06
                     [Jul]=07
                     [Aug]=08
                     [Sep]=09
                     [Oct]=10
                     [Nov]=11
                     [Dec]=12)
      
      function comparable_date {
          (( ${#1} < 11 )) && { echo date:time '"'$1'"' too short; return 42; }
          local day=${1:0:2} month=${1:3:3} year=${1:7:4} hms=${1:12}
          local nm=${mn[$month]}
          [[ $nm == "" ]] && { echo month '"'$month'"' not understood; return 44; }
          eval $2=\"$year-$nm-$day-$hms\"
      }
      
      (( $# < 3 )) && { echo Usage: ${0##*/} start-date end-date file; exit 45; }
      
      comparable_date $1 start || exit 46
      comparable_date $2 end   || exit 47
      
      while read date data; do
          comparable_date $date this &&
              [[ $this < $start || $this > $end ]] || echo $date $data
      done < $3
      Runs about 20 times faster on my computer. Note the tricky logic comparing the dates is because bash doesn't have ">=" or "<=" for strings.

      Regards, John Little
      Regards, John Little

      Comment

      Working...
      X