-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslides.txt
650 lines (519 loc) · 12.6 KB
/
slides.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
.
.
.
.
.
.
___ ___ __
/ \ \ / / |/ /
/ _ \ \ /\ / /| ' /
/ ___ \ V V / | . \
/_/ \_\_/\_/ |_|\_\
.
_____ ___ ___ __
|_ _|/ \ \ / / |/ /
| | / _ \ \ /\ / /| ' /
| |/ ___ \ V V / | . \
|_/_/ \_\_/\_/ |_|\_\
.
by
.
@mikepea
.
.
.
.
.
.
.
AWK? Wassat?
------------
.
@last
.
A data transformation and reporting tool.
Reads records from files/stdin, and interprets
each record as a list of fields which can then
be manipulated.
@last
.
Awk is a programming language designed to
make many common information retrieval and text
manipulation tasks easy to state and to perform.
( y/awk-1978-paper )
@last
.
Written in 1977, by Alfred Aho, Peter Weinberger,
& Brian Kerninghan,
@last
.
Hence AWK!
.
.
.
.
.
.
.
AWK? Why?
---------
.
.
@last
.
Well, I <3 Awk.
@last
.
First language I ever wrote anything 'commercial' in.
In 1994, :ahem:.
@last
.
But i'm not alone - y/cantrill-bigdata (skip to 19:30)
@last
.
THE [grey] AWK BOOK - y/awk-book
.
.
.
.
.
.
.
AWK? Why?
---------
.
.
.
grep ' 200 ' access_log | awk '{print $1}'
@last
.
:(
@last
.
But I always remember that it can do loads more!
.
A Bit Of History
----------------
.
@last
1970: Unics [sic] is born. Ken Thompson, Dennis Ritchie at Bell Labs.
Born out of multics, after much financial wrangling to get
a spare PDP-7.
@last
First step - Space Travel game! (Then copy, print, edit, delete :)
@last
.
1971: man pages appear, at the request of Doug McIlroy (their manager ;)
@last
.
1973: Unix pipelines invented (along with sed, grep and tr)
Effectively creating the Unix Philosophy, and 'an unforgettable
orgy of one-liners'. Doug McIlroy <3
@last
.
1975: Lex and Yacc are created, enabling tools like 'bc'
@last
.
1977: Awk is born. Essentially the first scripting language on Unix!
.
"Awk was originally designed... in part of an experiment to see
how the Unix tools grep and sed could be generalized to deal with
numbers as well as text... based on our interest in regular
expressions"
@last
.
1985: Brian Kerninghan creates (and still maintains!) New AWK (nawk).
This is on your Macs!
@last
.
1988: POSIX standard created, with awk as an included component.
.
.
.
.
.
.
.
.
.
.
.
.
The AWK Programming Language, 1988
----------------------------------
@last
.
.
<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3
<3 <3
<3 Typeset using a DEC VAX 8550 <3
<3 running 9th ed. UNIX® <3
<3 <3
<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3
.
.
The AWK Programming Language, 1988
----------------------------------
.
An absolute delight of a technical book.
@last
.
Still amazingly current. Awk has evolved (gawk, nawk)
but is still fundamentally the same language.
@last
.
Has got an 'Algorithms' section! Interview answers in AWK!
@last
.
Has a chapter on writing DSLs, which it calls
'Little Langauges' <3
@last
.
Has a section on creating a RDBMS in AWK, with a custom
query language!
.
.
Understand awk a bit betterer
-----------------------------
.
awk [-Fs] '{program}' input_files
awk [-Fs] -f {script} input_files
.
... where a program looks like:
@last
.
BEGIN {
# do initialisation stuff
}
.
conditional {
# do stuff on records matching condition
}
.
END {
# finish up. Like print a report.
}
.
.
Some important built-in variables
---------------------------------
.
@last
.
$1 .. $n - field references
$i - field referenced by integer i
@last
NF - number of fields in record
@last
$NF - last field in record
@last
.
NR - number of records read so far
('current line')
FNR - number of records read so far
in this file
@last
.
RS - input record seperator (default \n)
FS - input field seperator (default whitespace)
@last
.
ORS - output record seperator (default \n)
OFS - output field seperator (default whitespace)
.
.
An Unforgettable Orgy Of One-Liners
-----------------------------------
.
.
An Unforgettable(*) Orgy Of One-Liners
--------------------------------------
(*) jk, you're totally gonna forget these.
see github.com/mikepea/awk_tawk for a refresh
@last
.
awk 'END { print NR }'
@last
# wc -l
@last
.
awk 'NR == 10000000'
@last
# print 10,000,000th line
@last
.
awk '{ print $NF }'
@last
# print last field of every line
@last
.
awk '{ n = n + NF } END { print n }'
@last
# wc -w
@last
.
awk 'BEGIN { FS="" } { n = n + NF + 1 } END { print n }'
@last
# wc -c
@last
.
.
.
An Unforgettable(*) Orgy Of One-Liners
--------------------------------------
(*) jk, you're totally gonna forget these.
see github.com/mikepea/awk_tawk for a refresh
@last
.
awk '$9 == 200 { print $1 }' access_log
@last
# show IPs that had 200s from an Apache access log
@last
.
ls -l | awk '{ sum = sum + $5 } END {print sum}'
@last
# print the total bytes of a list of files
@last
.
awk 'BEGIN { FS=":" } $2 > max { max = $2 } END {print max}' passwd
@last
# print highest uid from passwd file
@last
.
awk '{ $1 = NR; print}'
# replace first field with line number
@last
.
awk '{ $2 = ""; print}'
# erase the second field
.
.
An Unforgettable(*) Orgy Of One-Liners
--------------------------------------
(*) jk, you're totally gonna forget these.
see github.com/mikepea/awk_tawk for a refresh
.
.
awk 'BEGIN { print "Hello World!" }'
@last
# the BEGIN is important!
@last
.
awk 'BEGIN { srand(); t=srand(); print t }'
@last
# date +'%s' # WHAAAAT!!?!1111!
.
@last
.
Thank @codymello for that one. POSIX!
.
.
What else?
----------
.
.
.
.
_ __ ___ __ _ _____ ___ __ ___| |
| '__/ _ \/ _` |/ _ \ \/ / '_ \/ __| |
| | | __/ (_| | __/> <| |_) \__ \_|
|_| \___|\__, |\___/_/\_\ .__/|___(_)
|___/ |_|
.
OMG! THEY'RE SO NEW AND EXCITING! (*)
@last
.
.
.
(*) actually invented in 1956 by Stephen Cole Kleen, a mathematician
.
... and then repurposed by Ken Thompson in 'ed', and by extension 'grep'
.
.
.
.
.
Regexps in AWK
--------------
.
* Basically no different to sed/grep. Yey standards!
@last
* I'm amazed at how little they've changed
since 1988
.
@last
* Useful in record matching conditionals
.
/regex/ {
# do stuff on records matching regex
}
.
@last
/from/,/to/ {
# do stuff on records between regexes
}
@last
.
* No submatch variables :(
i.e. (.+) => \1 in sed
.
.
.
Useful built-in functions
-------------------------
.
.
@last
sub(/regex/,s) # leftmost sub in $0
sub(/regex/,s,t) # leftmost sub in t
gsub(/regex/,s) # global sub in $0
gsub(/regex/,s,t) # global sub in t
.
@last
substr(s,p) # return suffix of s, at pos p
substr(s,p,n) # return substr of s, len n, at pos p
.
Useful built-in functions
-------------------------
.
.
@last
srand(s) # seed random number generator with 's'
rand() # return random number 0 <= n < 1
.
@last
index(s,t) # return pos of string t in s. 0 otherwise
length(s) # return length of string
match(s,/regex/) # return index of match or 0 if not.
.
@last
.
awk 'length($0) > 80' myprog.py
# print lines >80 chars
Useful built-in functions
-------------------------
.
.
@last
printf(fmt, expr-list)
sprintf(fmt, expr-list)
.
.
.
How the hell did I not know about printf?!
-----------------------------------------
.
.
@last
* Works just like regular printf / sprintf
.
awk '{ printf("Hello %s %s", $3, $4) }'
awk '{ fullname = sprintf("%s %s", $3, $4) }'
.
.
Arrays
------
.
.
* associative, so really a dictionary
.
* one dimensional, though the book gives
a workaround for multi-dimensional (and this may be a
feature of modern gawk?)
.
@last
arr["thing"] = value
.
@last
records[NR] = $0
.
@last
for (key in arr) {
print key, arr[key]
}
.
@last
split($1, arr, ",")
.
@last
if arr[key] > 500
delete arr[key]
.
.
Custom functions are a thing
----------------------------
.
.
* As you'd expect, but have probably never used.
@last
* variables are passed as a copy
* arrays are passed by reference
.
@last
function add(arg1, arg2) {
return arg1 + arg2
}
@last
.
function alen(a) {
c=0;
for (i in a) c++;
return c;
}
@last
.
function clear_array(a) { split("", a, ":"); }
@last
.
function get_key() { RS="\n"; getline key < "-"; RS="" }
.
.
Interation with other programs
------------------------------
.
.
@last
* system() works as you'd expect
.
system("cat " $2)
@last
.
function refresh() { system("clear"); print ""; }
@last
.
* also can use a pipe!
.
while ("who" | getline) {
num_users++
}
.
.
.
.
.
What's crappy about Awk?
------------------------
.
@last
* scope is global. Meh.
@last
.
* $0 is a string
@last
.
* OFS isn't as useful as I thought as a result :(
.
.
.
.
.
.
.
The AWK Programming Language, 1988
----------------------------------
.
"Awk is not a solution to every programming problem,
but it's an indispensable part of a programmer's
toolbox, especially on Unix, where easy connection
of tools is a way of life."
!awk '{print " ",$0}' present.awk