Tips for golfing in AWK

7

2

What general tips do you have for golfing in AWK? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to AWK. Please post one tip per answer.

RAM

Posted 2013-10-26T05:01:58.607

Reputation: 797

Answers

5

These are not in any particular order, and some of them might even apply to other languages too, but not to a lot of them I think.

  • You can sometimes use awk's simple string concatenation to assemble numbers

    z=x*10+y and a=b*10

    become

    z=x y and a=b 0

  • You can use the ~ operator (match pattern on the right side) to compare numbers or strings if you know that the two parts meet certain conditions

    for instance if you know that a will always be smaller than or equal to b, you can replace

    a==b

    with

    a~b

    or if you want to check for an empty string s (which means that s isn't allowed to be "0" either), and you haven's changed the variable X before, so that it is still undefined, you could use

    X~s

    instead of

    s==""

    if you want to check if i is an integer you can use

    i!~/\./ (i doesn't contain a dot)

    instead of

    i==int(i)

  • You can swap two numbers in a single command

    t=a;a=b;b=t

    becomes

    a+=b-(b=a)

    saving one character

  • If you don't need them as input anymore, you can use the input variables $1, $2, $3, ... as an array. So instead of writing a[n] you can just write $n. Sometimes even while reading the input from these you can use the already handled ones as a stack for something.

  • You can make good use of the built-in separators FS (space, if not changed by you) and RS (newline) when concatenating strings

    a=$1" "$2

    becomes

    a=$1FS$2

    or even better

    a=$1"\n"$2

    becomes

    a=$1RS$2

  • If there is no input and you use the BEGIN block, you can try to use the END block instead. Depending on the judge it works. If you test it, you have to press Ctrl-D (end of input) to access the END block.

  • If you want to skip the first line, you can use

    C++

    instead of

    NR>1

    if you won't be using C anywhere in your program.

Cabbie407

Posted 2013-10-26T05:01:58.607

Reputation: 1 158

1

To read and process a number on each line:

{
    n=$1;  
    print(n*n);
    // OR printf("%d\n",n*n);
}

Compressed form (Length = 14):

{print($1*$1)}       // thanks due to @manatwork

Shorter Code (Length = 7)

1,$0^=2              // thanks due to @llhuii

When compiled and run in gawk with inputs:

1
2
3

Output:

1
4
9

RAM

Posted 2013-10-26T05:01:58.607

Reputation: 797

2Why the variable? And why the parenthesis? {print$1*$1} is shorter. – manatwork – 2013-10-26T11:02:55.123

21,$0^=2 is shorter – llhuii – 2013-12-08T07:12:26.667

1

Techniques

  • Thanks to Peter Taylor for pointing out the language-generic version of this question for which I did not bother to search. I'll try to limit this answer to things that are not available in other C-like languages.

  • The frequently accessed builtin variables are 2 characters long. Find the break-even point for assigning to a temp variable. a=$1; is five characters, so it breaks even after five uses of $1;

  • Remember that the default action for a matching pattern is print $0, so if you get what you want printed into $0, just use

    1
    
  • Getting creative with operator ordering can save you some parentheses or extra statements

    for(i=1;i<=NF;i++)print$i;
    

    vs

    for(;i<NF;)print$++i;
    

Examples

  • A solution to Do We Sink or Swim? in 70 characters:

    n{for(;NF;NF--)s+=$NF;n--}NR==1{n=$1;p=$3}END{print p<s?"Swim":"Sink"}
    
  • A solution to The Floating Horde in 44 characters:

    {for(i=NF;i>1;){n=int($i/2);$i%=2;$--i+=n}}1
    

Summary

awk will probably never outgolf APL, Golfscript, J, or K, but you can quite consistently beat other high level languages.

laindir

Posted 2013-10-26T05:01:58.607

Reputation: 341

2

The question asks for tips which are somewhat specific to Awk. The ternary operator is included in the tips for all languages.

– Peter Taylor – 2014-03-17T20:35:38.277