APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

grep awk example script


What is this stuff?

If this isn't exactly what you wanted, please try our Search (there's a LOT of techy and non-techy stuff here about Linux, Unix, Mac OS X and just computers in general!):

Sed and awk can be cryptic. I much prefer Perl, as I think it is easier to understand the flow.

For example, in Perl, you could do:


#!/usr/bin/perl
$ok=0;
while (<>) {
if (/MARK/) {
  $ok=1;next;
}
print if $ok;
$ok=0;
}
 

The logic is identical to the awk script below, but I think it's easier to understand if you have no knowledge of either language.





From - Fri Jun 16 07:27:48 2000
Message-ID: <3945CF25.F79F60DE@idealgroup.com> 
Date: Mon, 12 Jun 2000 23:05:25 -0700
From: Darryl Krasman <darryl@idealgroup.com>
Newsgroups: comp.unix.sco.misc
Subject: Re: Newbie grep question
References: <m131cNG-000CnpC@egps.com> 

Nachman Yaakov Ziskind wrote:
> 
> ... any easy way to sed/awk/grep out the line AFTER the marker line?
> I.e., I have text like this:
> 
> junk
> junk
> MARK
> I want this line
> junk
> junk
> junk
> MARK
> I want this line
> junk
> junk
> MARK
> I want this line
> junk
> junk
> junk
> junk
> MARK
> I want this line
> MARK
> I want this line
> 
> and so forth? No particular patterns/line counts to rely on - just the
> presence of "MARK" lets me know that the next line is valuable.  I spent some
> time reading man pages, and managed to hit
> MEGO. :-(
> 
> SCO OSE 505
> 
> Thanks!

Assuming your data is in a file called "data" try this:
$ awk '/^MARK/ { OK = 1; continue } OK == 1 { print $0; OK = 0 }' data
-- 
Darryl
Ideal Computer Group Inc.





Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> sed/awk/grep out the line AFTER the marker line



Increase ad revenue 50-250% with Ezoic

Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





I can’t go to a restaurant and order food because I keep looking at the fonts on the menu. (Donald Knuth)





This post tagged: