Skriptit / merkkisiivous.sh

Etenkin sanalistoja yhdistellessä (hakiessa samaa sanaa tai sanaparia kahdelta eri listalta) on tarpeellista saada sanat suunnilleen samaan "formaattiin" ja merkkisiivous.sh on siihen tarkoitukseen laatimani edistyksellinen shell-skripti.

Vaatii toimiakseen paketin icu-devtools asennettuna.

Skripti on tässä:

#!/usr/bin/env bash
#
# HUOM! SKRIPTI VAATII TOIMIAKSEEN paketin icu-devtools asennettuna.
#
# Skripti muuttaa tiedoston isot kirjaimet pieniksi ja suurimman osan
# erikoismerkeistä, kyrillisistä kirjaimista, html-koodin jäänteistä ja
# "mojibakeista" tavallisiksi aakkosiksi tai aakkospareiksi (ß -> ss ja
# æ -> ae ja niin edelleen). Myös perusskandit muutetaan.
#
# Käyttöohje:
# 1. Tee tyhjä hakemisto
# 2. Laita haluamasi tiedosto nimellä tiedosto.txt siihen
# 3. Laita myös tämä skripti sinne nimellä merkkisiivous.sh
# 4. Kirjoita chmod +x merkkisiivous.sh
# 5. Kirjoita ./merkkisiivous.sh
#
# Alkuperäisestä tiedosto.txt:stä tehdään
# tiedosto_merkkisiivous_bak.txt -niminen varmuuskopio ensin. Itse
# tiedosto.txt muuttuu sisällöltään niin, että se siivotaan
# erikoismerkeistä ja isoista kirjaimista.
# 
# Tämän on alkujaan kyhännyt Toti joulukuussa 2025 ja tämä on
# päivitetty 4. versio (29.1.2026).
#
echo
echo "Vaihe 1/6"
echo "Luodaan varmuuskopio alkuperäisestä tiedostosta"
echo
cp tiedosto.txt tiedosto_merkkisiivous_bak.txt

echo
echo "Vaihe 2/6"
echo "Korjataan tavalliset suomalaiset mojibaket"
echo
sed -i -e 's/Ä/a/g' tiedosto.txt
sed -i -e 's/Ã…/a/g' tiedosto.txt
sed -i -e 's/ã¤/a/g' tiedosto.txt
sed -i -e 's/ä/a/g' tiedosto.txt
sed -i -e 's/Ã¥/a/g' tiedosto.txt
sed -i -e 's/Ö/o/g' tiedosto.txt
sed -i -e 's/ö/o/g' tiedosto.txt

echo
echo "Vaihe 3/6"
echo "Muutetaan kyrilliset kirjaimet latinalaisiksi"
echo
uconv -x cyrillic-latin tiedosto.txt -o tiedosto_temp1.txt
rm tiedosto.txt

echo
echo "Vaihe 4/6"
echo "Muutetaan erikoismerkit (ä, ö, ü ym.) latinalaisiksi kirjaimiksi (a-z)"
echo
iconv -f UTF-8 -t ASCII//TRANSLIT tiedosto_temp1.txt > tiedosto_temp2.txt
rm tiedosto_temp1.txt

echo
echo "Vaihe 5/6"
echo "Muutetaan kaikki kirjaimet pieniksi"
echo
tr '[:upper:]' '[:lower:]' < tiedosto_temp2.txt > tiedosto.txt
rm tiedosto_temp2.txt

echo
echo "Vaihe 6/6"
echo "Muutetaan HTML-koodi kirjaimiksi"
echo
sed -i -e 's/&#039;/’/g' tiedosto.txt
sed -i -e 's/&#192;/a/g' tiedosto.txt
sed -i -e 's/&#193;/a/g' tiedosto.txt
sed -i -e 's/&#194;/a/g' tiedosto.txt
sed -i -e 's/&#195;/a/g' tiedosto.txt
sed -i -e 's/&#196;/a/g' tiedosto.txt
sed -i -e 's/&#197;/a/g' tiedosto.txt
sed -i -e 's/&#198;/ae/g' tiedosto.txt
sed -i -e 's/&#199;/c/g' tiedosto.txt
sed -i -e 's/&#200;/e/g' tiedosto.txt
sed -i -e 's/&#201;/e/g' tiedosto.txt
sed -i -e 's/&#202;/e/g' tiedosto.txt
sed -i -e 's/&#203;/e/g' tiedosto.txt
sed -i -e 's/&#204;/i/g' tiedosto.txt
sed -i -e 's/&#205;/i/g' tiedosto.txt
sed -i -e 's/&#206;/i/g' tiedosto.txt
sed -i -e 's/&#207;/i/g' tiedosto.txt
sed -i -e 's/&#209;/n/g' tiedosto.txt
sed -i -e 's/&#210;/o/g' tiedosto.txt
sed -i -e 's/&#211;/o/g' tiedosto.txt
sed -i -e 's/&#212;/o/g' tiedosto.txt
sed -i -e 's/&#213;/o/g' tiedosto.txt
sed -i -e 's/&#214;/o/g' tiedosto.txt
sed -i -e 's/&#216;/o/g' tiedosto.txt
sed -i -e 's/&#217;/u/g' tiedosto.txt
sed -i -e 's/&#218;/u/g' tiedosto.txt
sed -i -e 's/&#219;/u/g' tiedosto.txt
sed -i -e 's/&#220;/u/g' tiedosto.txt
sed -i -e 's/&#223;/ss/g' tiedosto.txt
sed -i -e 's/&#224;/a/g' tiedosto.txt
sed -i -e 's/&#225;/a/g' tiedosto.txt
sed -i -e 's/&#226;/a/g' tiedosto.txt
sed -i -e 's/&#227;/a/g' tiedosto.txt
sed -i -e 's/&#228;/a/g' tiedosto.txt
sed -i -e 's/&#229;/a/g' tiedosto.txt
sed -i -e 's/&#230;/ae/g' tiedosto.txt
sed -i -e 's/&#231;/c/g' tiedosto.txt
sed -i -e 's/&#232;/e/g' tiedosto.txt
sed -i -e 's/&#233;/e/g' tiedosto.txt
sed -i -e 's/&#234;/e/g' tiedosto.txt
sed -i -e 's/&#235;/e/g' tiedosto.txt
sed -i -e 's/&#236;/i/g' tiedosto.txt
sed -i -e 's/&#237;/i/g' tiedosto.txt
sed -i -e 's/&#238;/i/g' tiedosto.txt
sed -i -e 's/&#239;/i/g' tiedosto.txt
sed -i -e 's/&#241;/n/g' tiedosto.txt
sed -i -e 's/&#242;/o/g' tiedosto.txt
sed -i -e 's/&#243;/o/g' tiedosto.txt
sed -i -e 's/&#244;/o/g' tiedosto.txt
sed -i -e 's/&#245;/o/g' tiedosto.txt
sed -i -e 's/&#246;/o/g' tiedosto.txt
sed -i -e 's/&#248;/o/g' tiedosto.txt
sed -i -e 's/&#249;/u/g' tiedosto.txt
sed -i -e 's/&#250;/u/g' tiedosto.txt
sed -i -e 's/&#251;/u/g' tiedosto.txt
sed -i -e 's/&#252;/u/g' tiedosto.txt
sed -i -e 's/&aacute;/a/g' tiedosto.txt
sed -i -e 's/&acirc;/a/g' tiedosto.txt
sed -i -e 's/&aelig;/ae/g' tiedosto.txt
sed -i -e 's/&agrave;/a/g' tiedosto.txt
sed -i -e 's/&aring;/a/g' tiedosto.txt
sed -i -e 's/&atilde;/a/g' tiedosto.txt
sed -i -e 's/&auml;/a/g' tiedosto.txt
sed -i -e 's/&ccedil;/c/g' tiedosto.txt
sed -i -e 's/&eacute;/e/g' tiedosto.txt
sed -i -e 's/&ecirc;/e/g' tiedosto.txt
sed -i -e 's/&egrave;/e/g' tiedosto.txt
sed -i -e 's/&euml;/e/g' tiedosto.txt
sed -i -e 's/&iacute;/i/g' tiedosto.txt
sed -i -e 's/&icirc;/i/g' tiedosto.txt
sed -i -e 's/&igrave;/i/g' tiedosto.txt
sed -i -e 's/&iuml;/i/g' tiedosto.txt
sed -i -e 's/&ntilde;/n/g' tiedosto.txt
sed -i -e 's/&oacute;/o/g' tiedosto.txt
sed -i -e 's/&ocirc;/o/g' tiedosto.txt
sed -i -e 's/&ograve;/o/g' tiedosto.txt
sed -i -e 's/&oslash;/o/g' tiedosto.txt
sed -i -e 's/&otilde;/o/g' tiedosto.txt
sed -i -e 's/&ouml;/o/g' tiedosto.txt
sed -i -e 's/&szlig;/ss/g' tiedosto.txt
sed -i -e 's/&uacute;/u/g' tiedosto.txt
sed -i -e 's/&ucirc;/u/g' tiedosto.txt
sed -i -e 's/&ugrave;/u/g' tiedosto.txt
sed -i -e 's/&uuml;/u/g' tiedosto.txt

echo
echo "Valmista! Tiedoston merkit siivottu."
echo