sed command for pattern search
Hi All,
I have a requirement to search for a particular pattern from the log files every 4 hours. I have set up a cron which runs the script every 4 hours. Log file pattern as below. 2018-12-17 01:53:47,390 [pool-3-thread-1] INFO [traceId=7c87f067fca636df,spanId=7c87f067fca636df] c.b.s.m.a.update.EmailProcessor - =========== Is email body HTML? 2018-12-17 04:47:21,838 [ActiveMQ Task-1] INFO [traceId=,spanId=] o.a.a.t.failover.FailoverTransport - Successfully connected to tcp://xx.xx.xx.xx:31313 After some research the below sed command was found Code:
sed -e "1,/^$(date -d -4hour +'%Y-%m-%d %H')/d" /home/nextStep/Logcheck.txt c.b.s.m.a.update.EmailProcessor How to append the pattern in the above code. Thanks for the help. |
looks like homework for me
you may try sed -n and add a second sed expression to look for the specified text. |
Some remarks:
|
Quote:
other programs/commands can search inside of files to find a match to, 'c.b.s.m.a.update.EmailProcessor' if true then log it true, if false then log it false. |
Quote:
I tried the below approach, but couldnot get the result expected . Code:
sed -e "1,/^$(date -d -1hour +'%Y-%m-%d %H')/d" | sed -n "c.b.s.m.a.update.EmailProcessor" /home/nextStep/Logcheck.txt |
let's review.
Quote:
2. job is to search log files looking for an entry "c.b.s.m.a.update.EmailProcessor' within same said log files. 3. If found ?? If not found ?? seding for a date within a file is redundant. The log files are already going to be four hours old, every 4 hours, entries are (maybe) added to all of the log files. So logic states what about the log files condition each 4 hours they are searched, again? plus your sed statment is malformed. grep has a return value sed does not, from what I've read on it thus far. grep -q grep retrun code Code:
The code 1 is because of no lines matching from the input. |
Quote:
If I were you I would do read sed man page (simply type "man sed" in your terminal) and/or do some additional searches on the internet. |
As has been asked, why sed? sed stands for stream editor. Your OP says
Quote:
IMO, sed is not the appropriate tool. I'd use grep, i.e. Code:
grep '.*c.b.s.m.a.update.EmailProcessor.*' /home/nextStep/Logcheck.txt |
Quote:
|
Quote:
|
C'mon folks, the OP is trying, let's help. And *YES* the command posted originally works exactly as it's supposed to.
Quote:
Code:
sed -e "1,/^$(date -d -1hour +'%Y-%m-%d %H')/d" /home/nextStep/Logcheck.txt | sed -n "/c.b.s.m.a.update.EmailProcessor/p" Code:
sed -e "1,/^$(date -d -1hour +'%Y-%m-%d %H')/d" -ne "/c.b.s.m.a.update.EmailProcessor/p" /home/nextStep/Logcheck.txt |
Code:
or Code:
grep -q "c.b.s.m.a.update.EmailProcessor" "$1" && \ Code:
$ ./findpatterengrep testfile |
Quote:
Quote:
And do not repeat the same grep. That is just wasting the resources and time. grep cannot handle the 4 hours requirement, sed can do the search for you |
Quote:
Code:
sed -n "1,/^$(date -d -1hour +'%Y-%m-%d %H')/d;/c.b.s.m.a.update.EmailProcessor/p" /home/nextStep/Logcheck.txt |
Quote:
That is a neither, here nor there, due to, if the user OP cannot figure out why it is not working due a space because of a copy paste using of answers in any forum, then it is a good exercise to be used as a learning tool, to test any code gotten from somewhere else before putting to use. Quote:
|
All times are GMT -5. The time now is 05:42 PM. |