Bruteforce file encoding using iconv

The other day I found a file with some weird encoding. It wasn’t UTF-8 or anything widely used to encode Polish characters like Ó and Ę.

To identify correct encoding I bruteforced it with the help of iconv using the following script:

for enc in `iconv -l`; do
  printf "$enc"
  res=$(iconv -f "$enc" -t UTF-8 testfile.txt)
  if [[ "$?" -eq 0 && "$res" == *"Ó"* && res == *"Ę"* ]]; then
    echo "OKOK: $enc" >> iconvresult.txt
    echo "$res" >> iconvresult.txt
  fi
done