Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • You shouldn't need to set doprint in BEGIN because all variables are automatically initialized to zero in awk. Commented Mar 31, 2021 at 20:56
  • It's not necessary but I like to show the initialization explicitly so that people who may not know how awk (in this case) works may be more likely to intuit how it works rather than just copying a working "black box". Commented Mar 31, 2021 at 22:47
  • Don't name a condition variable (doprint) based on the action you'll take when the condition is true (print the current line) as that tightly couples the condition to the action. Imagine your requirements change and now you need to print the opposite set of lines to the ones you're currently printing. Because you named your condition variable doprint you'd now have to change every line of code that references it as doprint would no longer make sense. If instead you named the condition variable found or similar then the only change you have to make is found==1 -> found==0 or similar Commented Mar 31, 2021 at 23:27
  • Alternatively, one could keep the tightly coupled name and simply change the predicate of the if statement which determines whether its value is truthy or falsy. Commented Apr 1, 2021 at 13:50
  • Then your "else" statement would be a double negative as it'd represent the condition where it is NOT true that $4 is NOT equal to marker and of course double negatives should always be avoided (which is also one of the reasons to avoid using a single negative at all when you could use a positive). Commented Apr 1, 2021 at 15:38