LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-19-2023, 04:47 AM   #1
nextStep
Member
 
Registered: Aug 2018
Posts: 32

Rep: Reputation: Disabled
grep for a pattern inside log


hi Team,

I have a big log file in the below format

2023-12-18 21:50:47,236 xx.xx.xx.net [http-nio-8080-exec-31] INFO [application=mock-service, traceId=03318e367d976a7f, spanId=0437430f6e81a6e1] a.b.c.d.e.f.g.h.i - Pricing SDR fare value : 4195.20 and current fare : 0 for sdr W7KXK3
2023-12-18 21:50:54,056 xx.xx.xx.net [http-nio-8080-exec-31] INFO [application=mock-service, traceId=03318e367d976a7f, spanId=0437430f6e81a6e1] a.b.c.d.e.f.g.h.i - Pricing SDR fare value : 4195.20 and current fare : 0 for sdr W7KXK3

The requirement is to fetch the values as below
Pricing SDR fare value : 4195.20 and current fare : 0 for sdr W7KXK3
Pricing SDR fare value : 4195.20 and current fare : 0 for sdr W7KXK3

But when i give the
Code:
zgrep  "Pricing SDR fare value" <log file>
The result have all the lines as below
2023-12-18 21:50:47,236 xx.xx.xx.net [http-nio-8080-exec-31] INFO [application=mock-service, traceId=03318e367d976a7f, spanId=0437430f6e81a6e1] a.b.c.d.e.f.g.h.i - Pricing SDR fare value : 4195.20 and current fare : 0 for sdr W7KXK3

How to get the exact match . I tried all possible combinations of zgrep and sed/awk but the result is printing all the lines.

Any help is appreciated
 
Old 12-19-2023, 04:53 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,335
Blog Entries: 3

Rep: Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731
It looks like the results are as expected. What do you mean by the exact match?

If you are looking to get only the matched string and not the time, date, and other context, then one way is to look at the -o option grep.

Code:
grep -o 'Pricing SDR fare value.*' logfile
If the pattern needs to get more complex look at the -E or -P options.'

As always, see "man grep" for the details.
 
1 members found this post helpful.
Old 12-19-2023, 05:10 AM   #3
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,483

Rep: Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556
Code:
cut -d" "
is your friend in this case, assuming lines always contain the same number of fields each time.
 
Old 12-19-2023, 05:16 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,146

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
Nope, regex rules. See post #2.
 
Old 12-19-2023, 05:19 AM   #5
nextStep
Member
 
Registered: Aug 2018
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thanks Turbocapitalist. That approach worked here.
 
  


Reply

Tags
grep



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
[SOLVED] grep pattern grep -Ff phpshell Programming 6 11-24-2013 11:31 PM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
substitute a pattern only if some other pattern is present raghu123 Programming 3 06-26-2009 06:53 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
replace a text pattern with the reverse of another text pattern lothario Linux - Software 5 07-25-2008 02:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 PM.

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