1

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.

3
  • Don't store command in a variable. You would need to use eval to use it. Commented Aug 30, 2016 at 7:46
  • can u please show me a example Commented Aug 30, 2016 at 9:07
  • 1
    Regex isn't the best tool to match date range. You'd have better luck parsing the log file names with a script. Here is an example showing how complicated a regex can become for dates Commented Aug 30, 2016 at 9:08

1 Answer 1

1

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. ^_^

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.