g | x | w | all
Bytes Lang Time Link
054regarding string concat freebies in awk250329T022752ZRARE Kpo
nanone of the great things about awk's sigils $ is that string concat can be even more condensed than having to use the builtins as buffer zones say221101T114130ZRARE Kpo
nan210522T070606ZPedro Ma
nanThese are not in any particular order150905T095917ZCabbie40
nan140317T175048Zlaindir
nan131026T050318ZRAM

regarding string concat freebies in awk, there are 5 scenarios where gapless concat is guaranteed to be safe (first 4 examples are attempting to either prepend or append some arbitrary string inside awk variable __)

  1. Immediately trailing numbers : e.g. 367 __ —> 367__

    — ditto for fields referenced by digits. e.g. $19 __ —> $19__

  1. Immediately before fields/sigils : e.g. __ $NF —> __$NF

    — to perform ++$NF or --$NF, do (__)++$NF instead.

  1. Immediately trailing array cells: e.g. ___[_] __ —> ___[_]__

    — this is mostly useful when performing array join with seps

  1. Immediately trailing closing parenthesis ) (like grouping pairs or function calls) : e.g. split(...) __ —> split(...)__, or (sumN - cntN) __ —> (sumN - cntN)__

    — the primary use case for this is to concat an empty string after the grouping, which would force convert a numeric zero (0) to string zero ("0") so it would evaluate to TRUE in any boolean context or pattern space. The alternative approach would be extra logic and verbosity to handle the edge case.

  1. The strangest one - conjuring up arbitrary chain of digits by concating the same variable repeatedly while altering its value along the way :
gawk -p- -be 'BEGIN { print _++_!_--_!_++_++_^_^_^_, _ }' 
     
01001165536 2
    # gawk profile, created Fri Mar 28 21:29:37 2025

    # BEGIN rule(s)

    BEGIN {
     1      print _++ _ !_-- _ !_++ _++ _^_^_^_, _
    }

— By end of the sequence, _ only has a measly value of 2, since it never stored 65,536 back into itself

one of the great things about awk's sigils $ is that string concat can be even more condensed than having to use the built-ins as buffer zones - say, u wanna prepend a zero to the full row:

 _=0
$_=_$_
jot -c 8 75  | gawk '$_=+_$_'  

0K
0L
0M
0N
0O
0P
0Q
0R

and you wanna make patterns out of it ?

jot -c 8 75  | gawk '$_=_++$_'  # integers

0K
L 1
M  2
N   3
O    4
P     5
Q      6
R       7

jot -c 8 75  | gawk '$_=_++$++_' # even numbers

K 0
L   2
M     4
N       6
O         8
P           10
Q             12
R               14

jot -c 8 75  | gawk '$_=++_$++_'  # odd numbers

K 1
L   3
M     5
N       7
O         9
P           11
Q             13
R               15

And honestly, what language can repeat stings THIS easily :

jot 20 | mawk 'NF=OFS=$_'

1
22
333
4444
55555
666666
7777777
88888888
999999999
10101010101010101010
1111111111111111111111
121212121212121212121212
13131313131313131313131313
1414141414141414141414141414
151515151515151515151515151515
16161616161616161616161616161616
1717171717171717171717171717171717
181818181818181818181818181818181818
19191919191919191919191919191919191919
2020202020202020202020202020202020202020

or can decode arbitrary precision hex with this few keystrokes :

echo 0xEDCFAB12EDCFAB127659438976594389EDCFAB | 
gawk -nM '$_=+$_'
5303367068685265828195859270035065456131166123

Truthy and falsey values

Boolean evaluation is somehow flexible in AWK, and this is awesome. Remember: AWK's basis is pattern{action}; when pattern is true, it executes action.

Besides doing their business, some built-in functions return values, e.g., split(), gsub(). They are also useful as a pattern when manipulating the input.

Examples of truthy and falsey variables

I tried to come up with valuable examples of how to exploit truthy/falsey variables. This is non-exhaustive. Anyone is encouraged to share more examples, and I would add them to this list.

Try it online!

"strings are truthy"{print 1}  # truthy; strings are always truthy, except for the null string

-0xF3e10{print 2} # truthy; numbers different from zero are truthy

0{print 3}  # falsey; zeroes are false: 0, -0, +0, 0x0, 0000... expect for "0", which is a string
0 b{print 4}  # truthy; now the number 0 is concatenated to a null string (a variable still not assigned), thus converting it to "0"
""{print 5} # falsey; null string
b{print 6}  # falsey; b is null (a variable not assigned)

a=@/x/{print 7} # truthy; defining a strongly typed regex constant. Different from a /x/ pattern
/x/{print 8}  # falsey; /x/ does not match the input

c["test"]{print 9}   # falsey; undefined item of array, null
c["test"]++{print 10} # falsey; variable is evaluated before increment
c["test"]{print 11}   # truthy; now, c["test"] equals 1
c["test2"]{print 12}  # falsey; although the c array now exists, the "test2" element does not

Result:

1
2
4
7
11

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.

Techniques

Examples

Summary

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

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