Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-02-2012
Registered User
 
Join Date: Apr 2010
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
Extract lines with specific words with addition 2 lines before and after

Dear all,

Greetings.

I would like to ask for your help to extract lines with specific words in addition 2 lines before and after these lines by using awk or sed.

For example, the input file is:


Code:
1 ak1 abc1.0
1 ak2 abc1.0
1 ak3 abc1.0
1 ak4 abc1.0
1 ak5 abc1.1
1 ak6 abc1.1
1 ak7 abc1.1
1 ak8 abc1.1
1 ak9 abc1.1
1 ak10 abc1.1
1 ak11 abc1.2
1 ak12 abc1.2
1 ak13 abc1.2
1 ak14 abc1.2
1 ak15 abc1.2

How can I extract the lines as below:


Code:
1 ak3 abc1.0
1 ak4 abc1.0
1 ak5 abc1.1
1 ak6 abc1.1
1 ak7 abc1.1
1 ak8 abc1.1
1 ak9 abc1.1
1 ak10 abc1.1
1 ak11 abc1.2
1 ak12 abc1.2

I truly appreciate your advice and helps. Thank you very much in advance. Hoping to hear a reply from you.

Best regards,
Amanda

Moderator's Comments:
Please use code tags next time for your code and data.
Sponsored Links
    #2  
Old 07-02-2012
...@...
 
Join Date: Feb 2004
Location: NM
Posts: 9,657
Thanks: 164
Thanked 646 Times in 623 Posts
What OS (type of UNIX)?
Sponsored Links
    #3  
Old 07-02-2012
Registered User
 
Join Date: Apr 2010
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
Dear jim mcnamara,

Sorry... Is x86_64 GNU/Linux the answer for your question?

I am sorry if I did not answer the questions, I am new in this field.

Thank you very much.

Amanda
    #4  
Old 07-02-2012
jayan_jay's Avatar
Forum Advisor
 
Join Date: Jul 2008
Posts: 831
Thanks: 9
Thanked 185 Times in 176 Posts
@amanda, your expected output shows, just a paragraph printing .. If yes, try the below ..

Code:
$ nawk '/ak3/,/ak12/' infile
1 ak3 abc1.0
1 ak4 abc1.0
1 ak5 abc1.1
1 ak6 abc1.1
1 ak7 abc1.1
1 ak8 abc1.1
1 ak9 abc1.1
1 ak10 abc1.1
1 ak11 abc1.2
1 ak12 abc1.2
$

If question varies, post the expected output along with your tries to achieve ..
Sponsored Links
    #5  
Old 07-02-2012
Registered User
 
Join Date: Apr 2010
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
Hi Jayan Jay,

Thank you very much for your reply.

Firstly I would like to apologize about my unclear example. Actually I have a big dataset which I need to extract lines with specific words from the 3rd column which is "abc1.1" from this example together with 2 lines above and 2 lines below. This pattern will loop for the whole dataset.

Is there any possibility?

Thank you very much in advance.

Best wishes,
Amanda
Sponsored Links
    #6  
Old 07-02-2012
itkamaraj's Avatar
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 Posts
Gnu grep has -C option

try this


Code:
grep -C 2 "abc1.1" input.txt

The Following User Says Thank You to itkamaraj For This Useful Post:
Amanda Low (07-02-2012)
Sponsored Links
    #7  
Old 07-02-2012
Registered User
 
Join Date: Apr 2010
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
Hi Itkamaraj,

Thank you very much for your help. It works perfectly.

Thanks again.

Amanda
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Display a specific words from a multiple lines Portabello UNIX Desktop for Dummies Questions & Answers 4 06-03-2011 04:33 PM
sed command to delete lines contain specific words neemoze HP-UX 5 12-07-2010 02:28 AM
Keep lines with specific words up in an order kolesunil Shell Programming and Scripting 6 11-19-2009 07:44 AM
Extract specific lines for graphing genehunter UNIX for Dummies Questions & Answers 1 06-21-2009 12:51 AM
Ignore some lines with specific words from file comparison jakSun8 Shell Programming and Scripting 2 03-13-2008 12:11 AM



All times are GMT -4. The time now is 07:58 PM.