Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, Im currently trying to make a script that can read through every line of a text file and compare a value to a day given by the user and ...
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    3

    Reading every line and counting

    Hi,

    Im currently trying to make a script that can read through every line of a text file and compare a value to a day given by the user and if it matches it will incease a variable by 1 and then move onto the next line and do the same,

    the text file is layed out like this,
    ipaddress day month daydate time timezone year

    I have managed to get this far

    Code:
    echo Please enter a day e.g. Mon
    read DAY
    NUMBER=0
    And now im not sure where to go, ive tried using a it statement but i got completely lost and ive also tried the awk thing but i don't know enough to figure it out.

    Can anyone point me in the right direction.

    Thanks

  2. #2
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    Check out the grep command UNIX man pages : grep ()
    Linux User #453176

  3. #3
    Just Joined!
    Join Date
    Apr 2009
    Posts
    3
    hi, thanks for the quick reply,


    The grep command works great, i have managed to count how many lines contain e.g. Mon, can it also be used to find if the time in the line is between a certain time range e.g. between 3pm and 5pm.

  4. #4
    Linux User vickey_20's Avatar
    Join Date
    Mar 2009
    Location
    Mumbai, India
    Posts
    493
    I think this might work out
    (grep 3) && (grep 5 )&& echo "this line contains both 3 and 5"

  5. #5
    Just Joined!
    Join Date
    Apr 2009
    Posts
    3
    hi,

    this is an example of one of the lines

    45.345.345.345 Mon May 04 12:03:26 GMT 2007

    is it possible to pull the date and time from the string and then find out what the scecific date and hour it is e.g. 2nd and the hour eg 06, and the we can probably us a if statement or something to compare if the hour matches the hour in the string.


    i am thinking something like awk would be able to do it as you can use $0 $1 $2 etc but not sure how you would get it to split the time into hour minute and seconds. Im guessing ther has to be a if statement in there as well.

    thanks

  6. #6
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    If you use awk to extract the time you can then use a substring extraction to spit the hour/min/seconds. To do a substring extraction use:

    Code:
    ${string:start_position:length}
    You will also find it useful to use a pipe to send the output of your awk into the substring extractor. BASH Programming - Introduction HOW-TO: Pipes
    Linux User #453176

  7. #7
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by Kieren View Post
    If you use awk to extract the time you can then use a substring extraction to spit the hour/min/seconds. To do a substring extraction use:

    Code:
    ${string:start_position:length}
    if using awk, he can do a split in awk as well, using split(). no need to specifically use the shell

Posting Permissions

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