|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
|
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
What OS (type of UNIX)?
|
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
||||
|
||||
|
@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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Hi Itkamaraj,
Thank you very much for your help. It works perfectly. Thanks again. Amanda |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|