Find the answer to your Linux question:
Results 1 to 4 of 4
I am running into an issue that I was trying to figure out on my own but I've sadly hit a brick wall. What I am trying to do is ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75

    date help

    I am running into an issue that I was trying to figure out on my own but I've sadly hit a brick wall.

    What I am trying to do is make a script smart enough to be able to take a day that a user inputs (such as Thursday) and search a file for not only the day the user entered but the day before the day the user entered. Then return all the lines that match that along with another variable. I was thinking of using grep to do the wonderful task of searching the file since I can specify -v to get an exact match.

    As an example:
    User types in Thursday (even though today is Friday). Script takes Thursday and adds Wednesday for grep to search with.

    I am also curious is grep or awk faster at searching files and reporting the criteria searched? Or are they about equal?

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    That shouldn't be too tough. You could even use an if-elif chain to figure out the day before. For instance:
    Code:
    echo -n "Enter the day to search for: "
    read today
    
    if [ "$today" = "Monday" ]; then
        yesterday=Sunday
    elif [ "$today" = "Tuesday" ]; then
        yesterday=Monday
    elif
    ...
    else
        echo 'You did not enter a valid date!'
    fi
    You would obviously want to normalize the string that they entered. I suggest forcing to lowercase and then possibly capitalizing the first letter. Your choice.

    As for the second question, I have no proof for my claim, but I would imagine that grep is faster. The only reason for this is that awk is an entire scripting language that is FAR more powerful than grep is. And initializing all of that is probably far more intensive than grep. So if you're not going to use that power, you may as well use grep.

    Also, you say that you would use "-v". "-v" actually inverts the match, to show all lines that _DON'T_ match the regular expression.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75
    Is there not a way to query the shell as well to get that information? I have the script already completed that will run based on the days and this script will actually be ran from a php script.

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    User types in Thursday (even though today is Friday). Script takes Thursday and adds Wednesday for grep to search with.
    Hi,

    This command gives a day before the given day:

    Code:
    date --date "Thursday -1 day" +%A
    gives:

    Code:
    Wednesday
    Regards

Posting Permissions

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