LinuxQuestions.org
Help answer threads with 0 replies.
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 07-01-2010, 12:44 AM   #1
linuxromeo
Member
 
Registered: Jun 2010
Posts: 45

Rep: Reputation: 15
Thumbs down Selective grep


Hi

I have to grep a certain entry in a file. But the thing is I need to grep only the first and third occurence of that pattern.

Eg: I have to grep "aaaa=" from the following file

aaaa=1
bbbb=2
dddd=2
fffaaaa=7
aaaa=3
ddaaaa=2
eeee=1


Now I only know "aaaa=" to search for "aaaa=1 and aaaa=3"

Thanks in advance for your help..
 
Old 07-01-2010, 12:59 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
It is not clear if the problem is to match the first and the third occurrences of the pattern in general or if 1st and 3rd are referred to this specific example (for example, the problem could be to match only the entire word and not the pattern as substring of another word). A real example would help to clarify.

Anyway, in the first case you can try something like:
Code:
grep aaaa file | sed -n '1p;3p'
whereas in the latter a simple
Code:
grep -w aaaa file
should do the trick.
 
Old 07-01-2010, 01:11 AM   #3
linuxromeo
Member
 
Registered: Jun 2010
Posts: 45

Original Poster
Rep: Reputation: 15
Thanks a lot problem solved....
 
Old 07-01-2010, 01:24 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by linuxromeo View Post
Thanks a lot problem solved....
I am just curious what the problem really was...
 
Old 07-01-2010, 02:21 AM   #5
linuxromeo
Member
 
Registered: Jun 2010
Posts: 45

Original Poster
Rep: Reputation: 15
For the time being...I just need to find the first and third pattern only...But I was just curious, if the given file is modified in future and the third pattern which I needed may become a 4th occurence or more...

eg:
aaaa=1
bbbb=2
dddd=2
fffaaaa=7
aaaa=6
wwaaaa=7
aaaa=3
ddaaaa=2
eeee=1

In this example...aaaa=3 has become the 5th occurence of the pattern "aaaa=".
So all I needed was to write a shell script generically for the same..
 
Old 07-01-2010, 02:48 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,472

Rep: Reputation: 2858Reputation: 2858Reputation: 2858Reputation: 2858Reputation: 2858Reputation: 2858Reputation: 2858Reputation: 2858Reputation: 2858Reputation: 2858Reputation: 2858
You can get rid of grep and just use all sed too:
Code:
sed -n '/aaaa/{1p;3p}' file
 
  


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 10:31 PM
selective scp horacioemilio Programming 3 01-10-2008 04:00 AM
How to allow selective access brokenpromises Linux - Security 3 07-31-2007 06:23 AM
selective printing out ovince Programming 2 04-28-2007 01:30 PM
How selective is your memory? xailer Linux - Newbie 12 12-03-2003 11:36 PM


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