LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 08-23-2007, 05:33 AM   #1
pgb_710
LQ Newbie
 
Registered: Aug 2007
Location: Pune
Posts: 10

Rep: Reputation: 0
Help on GREP


hi,

I want to grep the output between two patterns.
Ex: I have test file as follows:

$ cat test.txt
10.04 AM memory allocated
10:12 AM log started.
10:14 AM Table1 loaded.
10.24 AM Table2 loaded.
10.56 AM log stopped.
10.59 AM memory released.

I want the activities carried out between Log started and Log stopped using
GREP command.
I tried various combnation how ever failed to retrieve exact output.

I want following output:

10:14 AM Table1 loaded.
10.24 AM Table2 loaded.
 
Old 08-23-2007, 05:46 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I'd use sed, instead
Code:
sed -n '/log\ started/,/log\ stopped/p' test.txt | sed '1d' | sed '$d'
probably there is a better way... I'm not a sed guru!
 
Old 08-23-2007, 07:04 AM   #3
pgb_710
LQ Newbie
 
Registered: Aug 2007
Location: Pune
Posts: 10

Original Poster
Rep: Reputation: 0
Yes its working Fine

Quote:
Originally Posted by colucix View Post
I'd use sed, instead
Code:
sed -n '/log\ started/,/log\ stopped/p' test.txt | sed '1d' | sed '$d'
probably there is a better way... I'm not a sed guru!
Thanks much its working as expected..
 
Old 08-23-2007, 07:52 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You're welcome! Take in mind that the first sed extracts lines between the two regular expressions, the second and the third sed commands in the pipeline remove the first and the last line of the output, respectively. So, if you have more than one block starting with "log started" and ending with "log stopped", the lines containing these two strings - in the middle - will not be removed.
 
  


Reply



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
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM
grep: grep for two substrings? eur0dad Linux - General 2 08-17-2006 04:03 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 12:22 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

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