Skip to content

Instantly share code, notes, and snippets.

@Habetdin
Habetdin / .bashrc
Last active March 4, 2019 10:27
Bash Tweaks
# don't put duplicate lines or lines starting with space in the history
HISTCONTROL=ignoreboth:erasedups
# append to the history file, don't overwrite it
shopt -s histappend
# don't execute expanded result immediately
shopt -s histverify
# https://unix.stackexchange.com/a/18443
@Habetdin
Habetdin / 256-colors.bash
Created March 2, 2019 09:29
8-bit bash colors
#!/bin/bash
# https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
color_code=()
color_text=()
for c in {0..255}; do
color_code+=($(tput setaf $c))
color_text+=($(printf '%s=%i' "$(tput setaf $c | cat -v)" $c))
done
@Habetdin
Habetdin / antidupe.sh
Created March 4, 2019 10:35
Remove duplicates, but keeping only the last occurrence
# from the comments of https://backreference.org/2011/11/17/remove-duplicates-but-keeping-only-the-last-occurrence/
tac <src-file> | awk '!a[$0]++' | tac | tee <dst-file>

Keybase proof

I hereby claim:

  • I am habetdin on github.
  • I am habetdin (https://keybase.io/habetdin) on keybase.
  • I have a public key ASAUpULtmlGW5I2nIOxtEm1Rl0Ha0WvgfkzK9SbMfYJljgo

To claim this, I am signing this object:

@Habetdin
Habetdin / VPS.md
Created November 22, 2019 17:54
$25 for new UpCloud users
#
# Extracts exceptions from log files.
#
import sys
import re
from collections import defaultdict
REGEX = re.compile("(^\tat |^Caused by: |^\t... \\d+ more)")
# Usually, all inner lines of a stack trace will be "at" or "Caused by" lines.
# 1. Remove all containers including their volumes
docker rm -vf $(docker ps -a -q)
# 2. Remove all images
docker rmi -f $(docker images -a -q)
# (1) and (2) as an one-liner
export _DCKR=$(docker ps -a -q) && [ -n "$_DCKR" ] && docker rm -vf ${_DCKR[@]}; export _DCKR=$(docker images -a -q) && [ -n "$_DCKR" ] && docker rmi -f ${_DCKR[@]}; unset _DCKR
@Habetdin
Habetdin / newline.sh
Created November 22, 2021 12:42
Add a newline to the end of the files
# Based on https://unix.stackexchange.com/a/31955
find . -maxdepth 1 -name '*.cpp' -type f -exec sed -i -e '$a\' {} \;
@Habetdin
Habetdin / collapse-subnets.py
Last active March 19, 2022 23:27
Python script to minimize list of IPv4/IPv6 networks
#!/usr/bin/env python3
import ipaddress
import sys
source = []
result = []
for line in sys.stdin.readlines():
source.extend(ipaddress.ip_network(net) for net in line.split())
@Habetdin
Habetdin / convert-numerals.php
Last active March 6, 2023 15:13
Convert Eastern-Arabic, Bengali, Devanagari, Myanmar, Persian numerals to Western-Arabic (ASCII)
<?php
function normalizeNumerals($string) {
$numerals_ascii = range(0, 9); // Western Arabic
$numerals_arabic = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩']; // Eastern Arabic
$numerals_bengali = ['০', '১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯']; // Bengali
$numerals_devanagari = ['०', '१', '२', '३', '४', '५', '६', '७', '८', '९']; // Devanagari (Nagari)
$numerals_myanmar = ['၀', '၁', '၂', '၃', '၄', '၅', '၆', '၇', '၈', '၉']; // Myanmar (Burmese)
$numerals_persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']; // Persian
$string = str_replace($numerals_arabic, $numerals_ascii, $string);