moshem moshem - 2 years ago 61
Bash Question

using grep to find and write specific lines with conditions

I'm new to shell commands.
I need to do some advanced search script and I have no idea how to do it.

I need to look for lines in my computer log files that contained "str1" and not contained "str2", order by date that greater or equal to some giving date, and write the result to a file, where the process run in the background.

in more formal way I want to do this:

"Str1" && log_line
not contain
order by
having Date
>= %date%)

thank you!

Answer Source

You may be making it harder than it need to be. grep 'str1' "$logfile" | grep -v 'str2' will find all lines containing str1 and not str2. For the given date, you will want to touch a file that has the mod time set to the date you want to measure against. You then use your list to feed a while read -r fname; do loop and test [ "$fname" -nt "testfilename" ] and write the files that satisfy that condition to a new file.

Your final script flow will look something like:

touch -d "date string" testfilename
grep "$str1" "$logfile" | grep -v "str2" | while read -r fname; do
    [ "$fname" -nt "testfilename" ] && printf "%s\n" "$fname" > "$newname"
rm testfilename  ## clean up

There are other ways to approach this, but this is a fairly standard approach. Let me know if you have further questions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download