Regarding date command unix option %e

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • S Kajim
    New Member
    • Sep 2010
    • 3

    Regarding date command unix option %e

    I have a log file with contents within the file having the date format as follows on some lines:
    Sep 5, 2010

    Now, I am trying to run a script to parse through the log file. Since I want to grep all the entries for today's date, I use the date command:
    date '+%b %e, %Y'

    The problem is, %e should return "Sep(doublespac e)5, 2010" (notice double space after 'Sep' - I have not tried this as today is 30th September but just assumuing based on the date manual page which says the day will be preceeded by white space), thus it is not able to grep the exact string I want which is "Sep(singlespac e)5, 2010" (notice single space after 5). I thought this should be easy with some experts out there.

    Also, %d will not work since it preceeds the single digit days with a 0 (zero) whereas my log file does not have a preceeding 0 (zero) for single digit days.

    Also, would like to know if I can get rid of this by doing a sed command - would like to avoid it though. Please advice.
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    #2
    I tried few commands.

    This is my a.txt
    Code:
    Oct 1, 2010
    Oct 30, 2010
    Oct  1, 2010
    Oct  30, 2010
    So, my approach would be, don't rely on spaces inside the file.
    Separate month, day and year using date command and then apply the appropriate regex to search the pattern.

    In my case, I have done.

    Code:
    mon=`date '+%b'`
    day=`date '+%e'`
    yr=`date '+%Y'`
    
    grep -e "$mon.*$day.*$yr" a.txt
    It should produce,
    Code:
    Oct 1, 2010
    Oct  1, 2010
    I have put the simplest regex for grep, I don't know the consequences with your file.
    So change it accordingly.

    Comment

    Working...