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

grep awk example script


© December 2004 (various authors)

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


Inexpensive and informative Apple related e-books:

Take Control of Automating Your Mac

iOS 8: A Take Control Crash Course

Take Control of High Sierra

Photos for Mac: A Take Control Crash Course

Take Control of Parallels Desktop 12





Printer Friendly Version

Have you tried Searching this site?

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


Printer Friendly Version





Great minds discuss ideas; average minds discuss events; small minds discuss people. (Eleanor Roosevelt)




Linux posts

Troubleshooting posts


This post tagged:

AWK

Hardware

Troubleshooting



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode





SCO Unix Sales, Support, & Service

Phone:  707-SCO-UNIX (707-726-8649Toll Free: 833-SCO-UNIX (833-726-8649)
www.SCOsales.com