bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug-gawk] Lookahead pattern matching


From: Peng Yu
Subject: [bug-gawk] Lookahead pattern matching
Date: Wed, 9 Jan 2019 09:38:19 -0600

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]