LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-19-2006, 04:38 AM   #1
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,782

Rep: Reputation: 52
question about awk


printing a line with awk that maches a pattern is easy.

What I would like to do is print a line that matches a pattern and the next 4 lines from this line.

Any suggestions?

thanks
 
Old 07-19-2006, 05:12 AM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 31
I don't know about awk, but may I suggest GNU sed?
Code:
sed -n '/REGEXP/,+4 p'
 
Old 07-19-2006, 05:26 AM   #3
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
Maybe something along the following lines is what you need:

Code:
awk '
BEGIN {
  match_found = 0;
  line_ctr = 0;
}

{
  if ( match_found != 1 && $0 ~ /xxxxx/ ) match_found = 1;
  else if ( line_ctr < 4 ) { line_ctr++; }
}' "$1"
 
Old 07-19-2006, 02:15 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 33
or maybe
Code:
awk '{
     if( match($0,/REGEXP/) )
           {print ; getline; print; getline; print; getline; print; getline;print }
     }' filename
 
Old 07-19-2006, 02:34 PM   #5
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,782

Original Poster
Rep: Reputation: 52
thank you guys..
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Awk question blanny Linux - Software 6 05-02-2006 08:03 AM
awk question puishor Programming 2 08-26-2005 09:44 AM
AWK question paraiso Linux - Newbie 5 05-12-2005 01:37 PM
awk question jonlake Linux - Software 2 05-04-2005 03:58 PM
awk question dilberim82 Programming 2 10-01-2003 07:12 PM


All times are GMT -5. The time now is 02:58 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration