Nl

Add line numbers to a file (Four digits, leading zeros, separated by a colon):

nl -i1 -s': ' -nrz -w4 cotus.txt

awk '{printf"%04d: ",NR;print}' cotus.txt

Oh wait, you don’t want to count the blank lines?

awk '/[^ ]/{printf"%04d: ",++n}{print}' cotus.txt

Oh wait, you only want the first 30 lines?

awk '/[^ ]/{printf"%04d: ",++n}{print}NR==30{exit}' cotus.txt

Posted in Uncategorized | Leave a comment

Fgrep

Print line numbers of matches with #fgrep

fgrep -n Revenue cotus.txt

Posted in Uncategorized | Leave a comment

Djpeg

Convert a JPG to a 4-bit per pixel BMP:

djpeg -colors 16 teresita.jpg > teresita16.bmp

Posted in Uncategorized | Leave a comment

Absolute

Calculate absolute magnitude from visual magnitude for Sirius and Tau Ceti

#!/usr/bin/python3
import math
vmag = float(input("Visual magnitude?: "))
dist = float(input("Distance in light-years?: "))
amag = vmag-5*((math.log(dist/3.26156)/math.log(10))-1)
print ("Absolute magnitude=",amag)

 

Posted in Uncategorized | Leave a comment

LaTex

Okay, here’s a way ChatGPT can shine, teaching LaTeX :

R_{\mu\nu} - \frac{1}{2} g_{\mu\nu} R + \Lambda g_{\mu\nu} = \frac{8\pi G}{c^4} T_{\mu\nu}

Posted in Uncategorized | Leave a comment

Contour

Plot a function as a contour map

#!/usr/bin/python3
import matplotlib.pyplot as plt
import numpy as np
X, Y = np.meshgrid(np.linspace(-4, 4, 512), np.linspace(-4, 4, 512))
Z = (1 - X/2 + X**4 + Y**3) * np.exp(-X**2 - Y**2) * (1 - X/3 - Y **4) * (3 - Y + X **2)
levels = np.linspace(np.min(Z), np.max(Z), 20)
fig, ax = plt.subplots()
ax.contour(X, Y, Z, levels=levels)
plt.show()

 

Posted in Uncategorized | Leave a comment

Wash

Plot city population as a pie chart for Washington cities with more than 100,000 people

#!/usr/bin/python3
import matplotlib.pyplot as plt
labels = 'Seattle', 'Spokane', 'Tacoma', 'Vancouver', 'Bellevue','Kent', 'Everett', 'Renton', 'Spokane Valley', 'Federal Way'
sizes = [737015, 228989, 219346, 190915, 151854, 136588, 110629, 106785, 102976, 101030]
fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal')
plt.show()

Posted in Uncategorized | Leave a comment

Grep

Use grep to find adjacent duplicate words in standard input:

nl gettysburg.txt | grep -E “(\b\w+\b)\s+\1”

Posted in Uncategorized | Leave a comment

Deal

Deal four hands of five cards

#!/usr/bin/python
numbers= ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']*4
suits=['h','d','c','s']*13
cards=[x+y for x,y in zip(numbers, suits)]
import random
random.shuffle(cards)
print cards[1:6];print cards[7:12];print cards[13:18];print cards[18:23]

 

Posted in Uncategorized | Leave a comment

Curl

#Curl #Python

Get a list of links to Ukraine news non-interactively:

#!/usr/bin/python3

import re, sys

list = re.findall(r"https?:\S+", sys.stdin.read())
for m in list:
print (m, end="\n")
print()

—-

curl https://www.bing.com/news/search?q=ukraine | ./url.py | sort | uniq | sed '/microsoft/d'

 

Posted in Uncategorized | Leave a comment