LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 03-29-2010, 10:22 AM   #1
linutzy
LQ Newbie
 
Registered: Jul 2005
Posts: 18

Rep: Reputation: 0
A better grep


I have data inside a file that looks something like this:

---------------------------------------------------------

This.is.top/this.is.the.file.info.2010-03-26-07.gz:010101,010101:9899900
Car N USDIOUY F0000009248754308
Big y USDDELL R8982313804198034


This.is.another/this.is.the.file.info.2010-03-26-08.gz:01010,11111:99999
Bird Y USDIUYY T9876343978438794
Cat Y USD1234 U9362489736182374
Dog N USDDELL P8716234789126412
---------------------------------------------------------

I want it so that when I grep for a specific word I also get the top portion. Essentially, I need to grep in this case DELL, but my output should be like this:


This.is.top/this.is.the.file.info.2010-03-26-07.gz:010101,010101:9899900
Big y USDDELL R8982313804198034

This.is.another/this.is.the.file.info.2010-03-26-08.gz:01010,11111:99999
Dog N USDDELL P8716234789126412
------------------------------------------


I am able to do a simple grep for just:

Big y USDDELL R8982313804198034
Dog N USDDELL P8716234789126412


But I need the top section also, please and thanks.
 
Old 03-29-2010, 10:35 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,808

Rep: Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604
grep -B1

(man grep)
 
Old 03-29-2010, 03:11 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
awk is my weapon of choice in this case
Code:
$ awk 'BEGIN{RS=ORS="";FS=OFS="\n"} /DELL/ {printf "%s\n",$1;for(i=2;i<=NF;i++){if($i ~ /DELL/){printf "%s\n",$i}};printf "\n"}' file 
This.is.top/this.is.the.file.info.2010-03-26-07.gz:010101,010101:9899900
Big y USDDELL R8982313804198034

This.is.another/this.is.the.file.info.2010-03-26-08.gz:01010,11111:99999
Dog N USDDELL P8716234789126412
You can make it more flexible by using a shell-script
wrapper and putting DELL into a variable.



Cheers,
Tink

Last edited by Tinkster; 03-29-2010 at 03:14 PM.
 
Old 04-01-2010, 11:50 AM   #4
mahu_mahu
Member
 
Registered: Dec 2007
Location: Japan
Distribution: Debian
Posts: 42

Rep: Reputation: 3
Just FYI, you might want to read the section 4.1.3 of "Classic Shell Scripting" from O'REILLY.
 
  


Reply


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
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 08:12 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 05:56 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM


All times are GMT -5. The time now is 09:22 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration