LinuxQuestions.org
Review your favorite Linux distribution.
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
  Search this Thread
Old 10-31-2013, 04:36 PM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,171

Rep: Reputation: 60
GAWK Find Pattern Print Lines Before and After


Using grep I can easily use:

Code:
cvs log |grep -iB 10 -A 10 'date: 2013-10-30'
to display search results and 10 lines before and after. How can this be accompished using gawk?
 
Old 10-31-2013, 07:22 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
I can't really see why you would want to do it with awk, but
  • if countdown greater than 0
    • print $0
    • subtract 1 from countdown
    • next record
  • if pattern exists
    • print all elements in array
    • print $0
    • unset array
    • start countdown at 10
    • next record
  • if pattern not exist
    • if No of elements in array = 10
      • for element in 1 to 9,
        • array[element-1]=array[element];
      • array[9]="$0"
      • next record
    • add $0 to array
    • next record


^^ untested ( as well as unwritten as awk )
it can be done, but I don't know why since grep -C10 will do the same with less effort

NOTE:, there is probably a much better way of doing it in awk than my above 'logic'
 
Old 10-31-2013, 11:38 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,694

Rep: Reputation: 561Reputation: 561Reputation: 561Reputation: 561Reputation: 561Reputation: 561
Quote:
Originally Posted by metallica1973 View Post
Using grep I can easily use:

Code:
cvs log |grep -iB 10 -A 10 'date: 2013-10-30'
to display search results and 10 lines before and after. How can this be accomplished using gawk?
I changed the 10-line context to 3 lines for ease of testing.

With this InFile ...
Code:
line 01
date: 2013-10-30
line 03
line 04
line 05
line 06
line 07
line 09
line 10
line 11
date: 2013-10-30
line 13
line 14
line 15
line 16
line 17
line 18
line 19
date: 2013-10-30
line 21
line 22
line 23
date: 2013-10-30
line 25
date: 2013-10-30
line 27
line 28
line 29
line 30
line 31
line 32
line 33
date: 2013-10-30
... this gawk ...
Code:
gawk '{l[NR]=$0; if ($0~"date: 2013-10-30") {for (j=NR-3;j<=NR+2;j++) {a[j]=1}; a[j]=a[j]+99}}
   END{for (j=1;j<=NR;j++) {if (a[j]) print l[j]; if (a[j]>9) print "---" }}' $InFile >$OutFile
... produced this OutFile ...
Code:
line 01
date: 2013-10-30
line 03
line 04
line 05
---
line 09
line 10
line 11
date: 2013-10-30
line 13
line 14
line 15
---
line 17
line 18
line 19
date: 2013-10-30
line 21
line 22
line 23
date: 2013-10-30
line 25
date: 2013-10-30
line 27
line 28
line 29
---
line 31
line 32
line 33
date: 2013-10-30
Daniel B. Martin

Last edited by danielbmartin; 10-31-2013 at 11:50 PM.
 
1 members found this post helpful.
Old 11-07-2013, 10:12 PM   #4
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,171

Original Poster
Rep: Reputation: 60
thanks for the reply. You have left me speechless on how long it would take to use gawk instead of simply using grep.
 
  


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
print pattern matching lines until immediate occurence of a character keerthika Linux - Newbie 7 04-11-2012 06:58 AM
[SOLVED] Find pattern that extends over several lines porphyry5 Programming 3 06-12-2011 12:08 PM
Find pattern and comment out 2 lines after it ariszlo Linux - Newbie 3 10-14-2010 06:37 AM
[sed || gawk]: find and delete blocks and lines from file Hisu Programming 1 09-16-2008 03:01 PM
awk print lines that doesn't have a pattern huynguye Programming 5 05-04-2006 12:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:59 PM.

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