Question

I am trying to reuse a file called util.awk in other awk files

I would like to do the following:

util.awk

function ltrim(s) { sub(/^[ \t]+/, "", s); return s }
function rtrim(s) { sub(/[ \t]+$/, "", s); return s }
function trim(s)  { return rtrim(ltrim(s)); }

printTrim.awk

  @include "util.awk"
     BEGIN {
             printf("%s",trim("   test    "))
           }

I want to have a script util.awk included in the printTrim.awk in an elegant way.

I do not have gawk and it's NOT a n option to have it installed. I've heard there is a way to do this but I wasn't able to do that.

Appreciate

Was it helpful?

Solution

If there was a way to do it in all awks, GNU awk wouldn't have needed to invent a way to do it. The alternative is:

awk -f util.awk -f printTrim.awk
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top