The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Print block of lines matching a pattern vanand420 Shell Programming and Scripting 1 09-29-2008 05:09 AM
Pattern matching in Duplicated file and print once Danish Shakil Shell Programming and Scripting 1 08-01-2008 03:55 AM
Pattern Matching and lines after that kaushys Shell Programming and Scripting 4 06-23-2008 11:27 AM
pattern matching over more lines trek Shell Programming and Scripting 3 04-22-2008 06:37 AM
pattern matching and print with sed nymus7 Shell Programming and Scripting 2 04-14-2005 09:36 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-24-2008
naree naree is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 81
I want to print next 3 lines after pattern matching.

Dear Experts,
I have file called file1 in which i am greping a pattern after that i want to next 3 lines when that pattern is matched.

Ex:- file1

USA
UK
India
Africa
Hello
Asia
Europe
Australia
Hello
Peter
Robert
Jo
i want to next 3 lines after matching Hello pattern. i.e output should be.
Asia
Europe
Australia
Peter
Robert
Jo

Thanks & Regards
Naree
  #2 (permalink)  
Old 09-24-2008
navojit dutta navojit dutta is offline
Registered User
  
 

Join Date: Jul 2007
Location: Singapore
Posts: 32
hi....
Do you want the next three lines or all the lines after the pattern matching.
Your required output shows that you want all the lines after the pattern matching.
Be clear on your requirement first.
  #3 (permalink)  
Old 09-24-2008
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
using Sed:
Code:
sed -n '/Hello/ {n; p; n; p; n; p}' filename
  #4 (permalink)  
Old 09-25-2008
naree naree is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 81
It is saying unrecongnized command

Hi Yogesh,
When i use sed -n '/Hello/ (n; p; n; p; n; p)' file1 It is throwing unrecognized command. '/Hello/.. Can u help me in another way..
  #5 (permalink)  
Old 09-25-2008
Dabheeruz Dabheeruz is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 19
grep -A 3 "Hello" file.txt
  #6 (permalink)  
Old 09-25-2008
Dabheeruz Dabheeruz is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 19
Here is the same in perl:

Code:
#!/usr/bin/perl

open (FH,"blah.txt") || "die can't open file $!";

while (<FH>) {
        last if /Hello/ }

my ($curr, $next1, $next2, $next3)=<FH>;
close FH;

print "$curr$next$next2$next3";
  #7 (permalink)  
Old 09-25-2008
naree naree is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 81
grep -A is not working.

Dear Experts,
grep -A is not working in Solaris O.S. Please can anyone help in awk .

Thanks & Regards
Naree
Closed Thread

Bookmarks

Tags
context, grep

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:45 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0