moshem moshem - 6 months ago 13
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:

new_process
(
write
(
from
*log*
where
log_line
contain
"Str1" && log_line
not contain
"str2"
order by
Date
having Date
>= %date%)
to
read.txt)

thank you!

Answer

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:

newname="${1:-newfile.txt}"
touch -d "date string" testfilename
grep "$str1" "$logfile" | grep -v "str2" | while read -r fname; do
    [ "$fname" -nt "testfilename" ] && printf "%s\n" "$fname" > "$newname"
done
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.