Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-29-2010, 09:22 AM   #1
LQ Newbie
Registered: Jul 2005
Posts: 18

Rep: Reputation: 0
A better grep

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

Car N USDIOUY F0000009248754308
Big y USDDELL R8982313804198034,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:,010101:9899900
Big y USDDELL R8982313804198034,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, 09:35 AM   #2
Senior Member
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,823

Rep: Reputation: 611Reputation: 611Reputation: 611Reputation: 611Reputation: 611Reputation: 611
grep -B1

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

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
awk is my weapon of choice in this case
$ 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,010101:9899900
Big y USDDELL R8982313804198034,11111:99999
Dog N USDDELL P8716234789126412
You can make it more flexible by using a shell-script
wrapper and putting DELL into a variable.


Last edited by Tinkster; 03-29-2010 at 02:14 PM.
Old 04-01-2010, 10:50 AM   #4
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.


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

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

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