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 08-07-2013, 08:04 AM   #1
deepakdeore2004
LQ Newbie
 
Registered: Apr 2010
Posts: 26

Rep: Reputation: 0
Question grep text from a line in between "start" and "end" word


I want to grep the text which is shown in RED below. How can I do that?

0.0.0.0 - - [02/Aug/2013:12:03:12 +0000] "POST /some/url/some_file.shtml?THEME=one&STYLE=two&LANGUAGE=fr HTTP/1.1" 200 717 "http://some.url.com/some/url/public/some_other_file.shtml?some_tool_name=ToolName&tool_abc=fr-abc-def-ghi_40&tool_def=abc-ab_12345" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0" 0
 
Old 08-07-2013, 08:14 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by deepakdeore2004 View Post
I want to grep the text which is shown in RED below. How can I do that?

0.0.0.0 - - [02/Aug/2013:12:03:12 +0000] "POST /some/url/some_file.shtml?THEME=one&STYLE=two&LANGUAGE=fr HTTP/1.1" 200 717 "http://some.url.com/some/url/public/some_other_file.shtml?some_tool_name=ToolName&tool_abc=fr-abc-def-ghi_40&tool_def=abc-ab_12345" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0" 0
More information, please. What is your Start and End text? Are those keywords known to be unique within each line? If there are multiple Start/End pairs in any line, do you want all the intervening characters or only the first occurrence?

Daniel B. Martin
 
Old 08-07-2013, 08:18 AM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
You need to use grep twice:
Code:
grep -o 'tool_abc=[^&" ]*' input | grep -o '[^=]\+$'
Or add cut
Code:
grep -o 'tool_abc=[^&" ]*' input | cut -f 2 -d =
Or simply just use sed:
Code:
sed -n '/tool_abc=/{ s@.*tool_abc=@@; s@[&" ].*$@@; p; }' input

Last edited by konsolebox; 08-07-2013 at 08:28 AM. Reason: sed
 
2 members found this post helpful.
Old 08-07-2013, 08:23 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
^ konsolebox has a good presumption... the obvious solution for me (with very little information) would be to hard code it like so:
Code:
grep -o fr-abc-def-ghi_40 deepakdeore2004.txt

Last edited by schneidz; 08-07-2013 at 08:57 AM.
 
Old 08-07-2013, 08:44 AM   #5
deepakdeore2004
LQ Newbie
 
Registered: Apr 2010
Posts: 26

Original Poster
Rep: Reputation: 0
The highlighted word may change. I want the word which will be in between "&tool_abc=" and "&tool_def", this start and end will be unique.
 
Old 08-07-2013, 08:54 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
So with the commands I provided you could get tool_abc, and using the same methods you could also get tool_def. The questions is how do you want them as an output? An example of what you exactly intend to have would be really helpful.
 
Old 08-07-2013, 09:34 AM   #7
deepakdeore2004
LQ Newbie
 
Registered: Apr 2010
Posts: 26

Original Poster
Rep: Reputation: 0
The highlighted pattern is changing and I wanted that top 10 patterns by number of hits from the logs. Here is what I used for my requirement.

grep tool_abc log_file | grep -o 'tool_abc=[^&" ]*' | cut -f 2 -d = | sort | uniq -c |sort -nr |head
 
Old 08-07-2013, 09:45 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
You don't need the extra grep:
Code:
grep -o 'tool_abc=[^&" ]*' log_file | cut -f 2 -d = | sort | uniq -c | sort -nr | head
 
  


Reply

Tags
grep, perlscript, sed bash



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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
[SOLVED] sql: put "beginning of line" and "end of line" within a charset... masavini Programming 7 09-19-2012 08:03 AM
Using grep that matches only just a word with no "-" at the end sebelk Linux - General 3 08-13-2009 01:02 PM
fdisk reports odd "Start "and "End" sectors on single partition eponymous Linux - Software 3 10-01-2007 03:41 PM

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

All times are GMT -5. The time now is 09:46 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