Anant Tyagi
Anant Tyagi

Reputation: 71

Searching logs in UNIX between To and FROM date in shell script

I have a script that searches logs directory using grep command, below is the command.

export CMD="grep -ie '.*${RUN_DATE}.*${MESSAGE_TO_SEARCH}' ${LOG_FILE_PATH}/${LOG_FILE} | awk -F 'addInfo' '{print \$2;}' | sed '/^$/d'"

Now RUN_DATE is any date on which the script is run.

MESSAGE_TO_SEARCH is any message that we are searching in logs file.

It is declared before starting the script.

I want to modify the script that it searches between TO and FROM time, for eg:

FROM_TIME=2016-08-28 13:30:00 and

TO_TIME=2016-08-29 17:00:00

So the script should give logs that lies within this time only.

I tried the following by using passing dates as regex, but it didn't work.

export CMD="grep -ie '.*[${FROM_DATE}-${TO_DATE}].*${MESSAGE_TO_SEARCH}' ${LOG_FILE_PATH}/${LOG_FILE} | awk -F 'addInfo' '{print \$2;}' | sed '/^$/d'"

Please suggest correct method. Thanks

EDIT1: Please tell me a way i can do this, if regex is not appropriate for it.

Upvotes: 1

Views: 227

Answers (1)

diov
diov

Reputation: 11

If you just want to select the lines from a log file, there is another way with some simple commands but more readable.

1.Get the line numbers of the first and the last line.

# SN for start line number, EN for end line number.

export SN=`cat -n LOG_FILE | grep START_TIME | head -1 | awk '{print $1}'`
export EN=`cat -n LOG_FILE | grep END_TIME | tail -1 | awk '{print $1}'`

2.Get the lines you want.

head -$EN LOG_FILE | tail -$(($EN-$SN+1))

Once we know the line number we need, we can use head and tail to cut off the part we don't need. ^_^

Upvotes: 1

Related Questions