Print all the four letter words in the Bible:
perl -ne 'for(/\w+/g){$w=lc; $seen{$w}=1 if length$w==4} END{print"$_ " for sort keys %seen}' kjv.txt
Print all the four letter words in the Bible:
perl -ne 'for(/\w+/g){$w=lc; $seen{$w}=1 if length$w==4} END{print"$_ " for sort keys %seen}' kjv.txt
Calculate factorials over a range
#!/usr/bin/env python3
import sys
import functools
start = int(sys.argv[1])
end = int(sys.argv[2])
for n in range(start, end + 1):
factorial = 1 if n == 0 else functools.reduce(
lambda x, y: x * y, range(1, n + 1)
)
print(f"{n}! = {factorial}")
#!/usr/bin/python3 import matplotlib.pyplot as plt import numpy as np axes = [5, 5, 5] data = np.ones(axes, dtype=bool) alpha = 0.9 colors = np.empty(axes + [4], dtype=np.float32) colors[0] = [1, 0, 0, alpha] # red colors[1] = [0, 1, 0, alpha] # green colors[2] = [0, 0, 1, alpha] # blue colors[3] = [1, 1, 0, alpha] # yellow colors[4] = [1, 1, 1, alpha] # white fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.voxels(data, facecolors=colors, edgecolors='grey') plt.show()
Reformat standard input to any width
#!/usr/bin/python3
import sys
import textwrap
w=int(sys.argv[1])
for line in sys.stdin:
print(textwrap.fill(line,width=w))
bumpgen.bas
5 DEF SEG=&HB800 10 SCREEN 2:CLS 15 LINE (0,0)-(639,199),1,BF 20 DIM Y(639):FOR Z=0 TO 639:Y(Z)=999:NEXT Z 25 C=9.800000E-03:B=25:F=9.850000E-03 30 FOR Z=200 TO 0 STEP -3 40 FOR X=0 TO 639:A=A+C:J=Z+SIN(A)*B:IF J>Y(X) THEN 1000 50 Y(X)=J:PSET (X,J),0 1000 NEXT X:B=B*8.999999E-01:C=C+F:NEXT Z
Generate magic squares for any odd side length with #Python
#!/usr/bin/env python3
import sys
def magic_constant(n):
return n * (n * n + 1) // 2
def magic_square(n):
square = [[0] * n for _ in range(n)]
num = 1
i, j = 0, n // 2
while num 1 else 3
square = magic_square(n)
width = len(str(n * n))
print(f"Magic constant for n={n}: {magic_constant(n)}\n")
for row in square:
print(" ".join(f"{x:>{width}}" for x in row))
Combinations of r elements chosen from a pool of n elements with awk
BEGIN {
for (i=1; i <= r; i++) {
A[i] = i
if (i < r ) printf i OFS
else print i}
while (A[1] = 1; i--) {
if (A[i] < n - r + i) {
A[i]++
p = i
break}}
for (i = p + 1; i <= r; i++) A[i] = A[i - 1] + 1
for (i=1; i <= r; i++) {
if (i < r) printf A[i] OFS
else print A[i]}}
exit}
$ awk -v r=3 -v n=5 -f comb.awk
xxd style hex dumper
#!/usr/bin/python3
import sys
with open(sys.argv[1], "rb") as f:
data = f.read()
for offset in range(0, len(data), 16):
b = data[offset:offset + 16]
left = " ".join(f"{x:02x}" for x in b[:8])
right = " ".join(f"{x:02x}" for x in b[8:])
hexpart = f"{left:<23} {right:<23}"
text = "".join(chr(x) if 32 <= x <= 126 else "." for x in b)
print(f"{offset:08x} {hexpart} |{text}|")
Use awk to make your Mastodon follow list into an RSS feed
awk -F'[@,]' '{print "https://" $2 "/@" $1 ".rss"}' following_accounts.csv