wangp / bower

A curses terminal client for the Notmuch email system
Other
119 stars 11 forks source link

Awk script #101

Closed jgarte closed 2 years ago

jgarte commented 2 years ago

How does this awk script work generally? Sorry if this is somewhat off topic.

I was just trying to understand what exactly make_man does in the bower.1 Makefile target

BEGIN {
    output = 1;
}

/^See some.*screen shots/ {
    next;
}

/^Requirements$/ {
    output = 0;
}

/^Configuration$/ {
    output = 1;
}

output {
    print
}
wangp commented 2 years ago

It just drops some irrelevant sections from the man page. Briefly, awk implicitly loops over all the "records" in the input file, which by default are lines. The input is the README.md file.

BEGIN {
    output = 1;
}

At the start of the program, initialise a variable.

/^See some.*screen shots/ {
    next;
}

If the current line matches this regexp, skip to the next line.

/^Requirements$/ {
    output = 0;
}

If the current line matches this regexp, set output to 0 (false).

/^Configuration$/ {
    output = 1;
}

If the current line matches this regexp, set output to 1 (true).

output {
    print
}

If output is true, print out the current line.

jgarte commented 2 years ago

@wangp Awesome! Thanks for the explanation. It is really much appreciated.

I also explored the script a bit now to see the differences. I had forgotten to try that for my understanding:

diff -u README.md README_awked.md | diffr | head -n 80

-- diffr