LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-17-2018, 05:50 AM   #1
nextStep
Member
 
Registered: Aug 2018
Posts: 32

Rep: Reputation: Disabled
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
But the issue here is , it outputs all the text in the last 4 hours. My requirement is to find if the below pattern exists in the last 4 hours.
c.b.s.m.a.update.EmailProcessor

How to append the pattern in the above code.

Thanks for the help.
 
Old 12-17-2018, 06:00 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,791

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
looks like homework for me
you may try sed -n and add a second sed expression to look for the specified text.
 
Old 12-17-2018, 06:40 AM   #3
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Some remarks:
  • you do not need switch -e if you only have one sed script
  • I'm not sure why you use instruction d...
  • OK, I see you are using a regex as a line range end. Do you know that the end line number will be the first one that is going to match your regex (maybe not what you want if there are multiple logs in the last 4-hour span), and if there is no match, then every lines will be taken into account?
  • do you expect a "yes"/"no" output? If you want that, maybe awk would be more appropriate...
 
Old 12-17-2018, 07:11 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by nextStep View Post
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
But the issue here is , it outputs all the text in the last 4 hours. My requirement is to find if the below pattern exists in the last 4 hours.
c.b.s.m.a.update.EmailProcessor

How to append the pattern in the above code.

Thanks for the help.
do you have to use sed?
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.

Last edited by BW-userx; 12-17-2018 at 07:27 AM.
 
Old 12-17-2018, 08:07 AM   #5
nextStep
Member
 
Registered: Aug 2018
Posts: 32

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
looks like homework for me
you may try sed -n and add a second sed expression to look for the specified text.
Hi

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
 
Old 12-17-2018, 08:25 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
let's review.
Quote:
Originally Posted by OP
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.

My requirement is to find if the below pattern exists in the last 4 hours.
c.b.s.m.a.update.EmailProcessor
1. set up a cron job to fire off every 4 hours.
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.
Also to read on EXIT CODES on man grep page, EXIT STATUS 
Normally the exit status is 0 if a line is selected, 1 if 
no lines were selected, and 2 if an error occurred. The 
exit code is 1 because nothing was matched by grep.

Last edited by BW-userx; 12-17-2018 at 08:56 AM.
 
Old 12-17-2018, 08:38 AM   #7
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by nextStep View Post
Hi

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
Indeed, neither your first command, nor the 2nd one can work as is...
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.
 
Old 12-17-2018, 10:29 AM   #8
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,724

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
As has been asked, why sed? sed stands for stream editor. Your OP says
Quote:
I have a requirement to search for a particular pattern
(emphasis added).
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
Yes, that will show you the same lines every time it runs...not just the lines added in the last four hours. You'll need to analyze the result and parse it some more to tune the result.
 
Old 12-17-2018, 11:17 AM   #9
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by scasey View Post
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
No need to use .* as boundaries for your grep pattern

Last edited by l0f4r0; 12-17-2018 at 11:18 AM.
 
Old 12-17-2018, 11:31 AM   #10
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,724

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by l0f4r0 View Post
No need to use .* as boundaries for your grep pattern
I knew that...my bad...don't know what I was thinking. Thanks for pointing it out...better for others.
 
Old 12-17-2018, 05:01 PM   #11
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
C'mon folks, the OP is trying, let's help. And *YES* the command posted originally works exactly as it's supposed to.
Quote:
Originally Posted by nextStep View Post
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
Close - you need the input file on the first sed, and by using "-n" in the second sed you are suppressing all printing, so you have to explicitly print the lines you want. Try it like this.
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"
As suggested above, it is possibly better to only call sed once
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
 
Old 12-17-2018, 05:31 PM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Code:
grep -q "c.b.s.m.a.update.EmailProcessor" " /home/nextStep/Logcheck.txt"  && echo "yes" || echo "no"
works off return code 1 | 0

or

Code:
grep -q "c.b.s.m.a.update.EmailProcessor" "$1"  && \
echo "yes found  $(grep -o "c.b.s.m.a.update.EmailProcessor" "$1" | wc -l ) times" || \
echo "no"
results.
Code:
$ ./findpatterengrep testfile
yes found  3 times
placed within a bunch of text 3 times, and returned 3 times it was found. Then one could just do the math on each run to see the increments in finds.

Last edited by BW-userx; 12-17-2018 at 05:41 PM.
 
Old 12-18-2018, 02:40 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,791

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
Quote:
Originally Posted by BW-userx View Post
Code:
grep -q "c.b.s.m.a.update.EmailProcessor" " /home/nextStep/Logcheck.txt"  && echo "yes" || echo "no"
there is a space before /home in filename, which make this command useless.
Quote:
Originally Posted by BW-userx View Post
Code:
grep -q "c.b.s.m.a.update.EmailProcessor" "$1"  && \
echo "yes found  $(grep -o "c.b.s.m.a.update.EmailProcessor" "$1" | wc -l ) times" || \
echo "no"
use grep -c instead of grep | wc
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
 
1 members found this post helpful.
Old 12-18-2018, 02:42 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,791

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
Quote:
Originally Posted by syg00 View Post
As suggested above, it is possibly better to only call sed once
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
and probably works:
Code:
sed -n "1,/^$(date -d -1hour +'%Y-%m-%d %H')/d;/c.b.s.m.a.update.EmailProcessor/p" /home/nextStep/Logcheck.txt
 
Old 12-18-2018, 07:29 AM   #15
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by pan64 View Post
there is a space before /home in filename, which make this command useless.
This was not posted for copy paste efficiency. I just tossed that in here after I pasted it from my test code, that is where the space came from. It was "$1" to use the CLI.

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:
Originally Posted by pan64 View Post
Use grep -c instead of grep | wc
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
point taken. As one little file is not a good test for reality purposes.

Last edited by BW-userx; 12-18-2018 at 07:30 AM.
 
  


Reply

Tags
log files, pattern matching, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed delete lines with pattern to pattern (exluding the second) Jykke Linux - Software 10 07-23-2018 02:43 AM
vi search multiple pattern and remove only exact matching pattern amateurscripter Linux - Newbie 4 05-07-2018 01:19 PM
Search and replace Pattern preceeding another pattern nbkisnz Programming 3 05-13-2012 01:50 PM
Sed command stuff - search for pattern comvat25 Programming 2 10-03-2010 11:23 PM
[SOLVED] /bin/bash if statement pattern search, end of pattern special character? headhunter_unit23 Programming 3 04-29-2010 08:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration