LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-03-2014, 06:34 AM   #1
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 389

Rep: Reputation: 32
Sed print only the line that exists two lines after the match


Hello. I have this content in a file.

name: george
tag: genomes
ID: 455031

I want to get the line with the name by using sed/awk/grep.

I tried that command

sed -n '/455031/{g;1!p;};h' IDs.txt

which gave me the line with tag : genomes.

How can i make it to print only the line with name: george ?

Thank you.
 
Old 12-03-2014, 08:42 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Why not save into the buffer when you hit 'name' and then when you hit desired result, swap the buffer and print the name.
 
Old 12-03-2014, 02:12 PM   #3
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 389

Original Poster
Rep: Reputation: 32
Sorry. That's my fault

The file contains more than one records of that type.

name: george
tag: genomes
ID: 455031
name: Tom
tag: genomes
ID: 455032
name: Mary
tag: genomes
ID: 455034
name: Jack
tag: genomes
ID: 455036

So i want to extract that info through ID
 
Old 12-03-2014, 02:29 PM   #4
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 389

Original Poster
Rep: Reputation: 32
Finally worked with awk

Code:
awk '{a[NR%3]=$0}/455036/{print a[(NR-2)%3]}' IDs.txt
 
Old 12-03-2014, 05:28 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,161

Rep: Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943
Relies on very structured data - and no blank lines for example. Why not something like
Code:
awk '{a[$1]=$0}/455036/{print a["name:"]}' IDs.txt
 
Old 12-03-2014, 06:46 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
And the sed solution I mentioned:
Code:
sed -n '/name/{h};/455036/{x;p}' IDs.txt
 
  


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
Sed issue with finding a match on two sequential lines and deleting the previous line ulto Programming 3 10-26-2014 06:26 PM
[SOLVED] SED/AWK - Delete all lines until empty line is found after pattern match vikas027 Programming 13 03-28-2012 08:33 AM
[SOLVED] sed/awk to print every before line that pattern match niharikaananth Linux - Newbie 10 02-22-2012 10:47 PM
[SOLVED] sed: Match one line, make a substitution a few lines down? ShadowCat8 Programming 6 06-08-2011 07:59 PM
Sed/Awk: print lines between n'th and (n+1)'th match of "foo" xaverius Programming 17 08-20-2007 11:39 AM


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