Find all strings that start with a digit and contain at least one letter in any regular file under your home directory. A token is defined as a maximal sequence of alphanumeric characters and underscores. Collect unique tokens, sort them by the numeric value of their leading digits, and write them one per line to a file named results.txt. Deliverable: results.txt in the current directory containing the sorted tokens.
Steps:
Open a terminal and ensure you are in your home directory: cd ~
Create a small Python script that scans your home directory and extracts the tokens as described (robust against binary files and non-UTF-8 text):
python
import os
home = os.path.expanduser('~')
tokens = set()
def lead_num(s):
i = 0
while i < len(s) and s[i].isdigit():
i += 1
return int(s[:i]) if i > 0 else 0
def clean_token(raw):
return ''.join(ch for ch in raw if ch.isalnum() or ch == '_')
for root, dirs, files in os.walk(home, followlinks=False):
for f in files:
path = os.path.join(root, f)
try:
with open(path, 'r', encoding='utf-8', errors='ignore') as fh:
for line in fh:
for raw in line.split():
t = clean_token(raw)
if t and t[0].isdigit() and any(ch.isalpha() for ch in t):
tokens.add(t)
except Exception:
pass
out = sorted(tokens, key=lead_num)
with open('results.txt', 'w') as fh:
for t in out:
fh.write(t + '\n')
print(len(out))
Run the script and inspect the produced file:
bash
python3 extract_digit_tokens.py
cat results.txt | head -n 20
Deliverable: a file named results.txt containing one token per line, sorted by the numeric value of their leading digits.