[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] Lookahead pattern matching
From: |
arnold |
Subject: |
Re: [bug-gawk] Lookahead pattern matching |
Date: |
Thu, 10 Jan 2019 00:31:51 -0700 |
User-agent: |
Heirloom mailx 12.5 7/5/10 |
There is no look-ahead pattern matching method, nor is there likely to be.
The way you've done this is pretty standard for awk. IIRC it's referred to
as "control-break programming".
A different option is to read all the lines into an array and then
process them in a loop in the END rule. That may make your code
flow more easily.
As a side point, you would be better off using the -v option to set
the regex variable on the command line than reading it with getline in
the way you've done.
Arnold
Peng Yu <address@hidden> wrote:
> Hi,
>
> I want to look at future lines to determine what to do with the current line.
>
> In the following example, I want to check if the next line starts with
> "a" to decide whether to print the current line with a newline or not.
>
> I think lookahead pattern matching is a general class of problems. But
> the awk code does not look very nice, users need to do the lookahead
> on their own.
>
> Is there a more elegant way to program for such class of problems? Thanks.
>
> $ awk -f ./main.awk <(printf '%s' '^a') <<EOF
> A
> aB
> aC
> D
> EOF
> printf '%s' '^a'
> AaBaC
> D
>
> ########
> BEGIN {
> OLDRS=RS
> RS="^$"
> getline regex < ARGV[1]
> delete ARGV
> RS=OLDRS
> if(getline) {
> seen_last = 1
> last = $0
> }
> }
> {
> if($0 ~ regex) {
> printf("%s", last)
> } else {
> print last
> }
> last = $0
> }
> END {
> if(seen_last) print last
> }
>
> --
> Regards,
> Peng