DEV Community

Discussion on: 6 Unix tools you need to know about

Collapse
 
josephj11 profile image
Joe

I don't use tr very often, but it's very handy for getting rid of odd binary codes in text files and translating between upper and lower case. I have seen much fancier uses of it, but don't recall any at the moment.

Here's a quick way to show someone the format of a data file without revealing any sensitive data.

#!/bin/bash
## Obsfucate a text file
## 11/25/2013
## Thanks to D. Joe on the WNYLUG list
## For sharing the format of a text file with sensitive data in it

if [ -z "${1}" ]
then
  IN="/dev/stdin"
else
  if [ ! -r "${1}" ]
  then
    echo "Can't read [${1}]"
    exit 1
  fi

  IN="${1}"
fi

if [ -z "${2}" ]
then
  OUT="/dev/stdout"
else
  OUT="${2}"
fi

tr 'a-z' 'A-Z' < "${IN}" | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'AAAAAAAAAAAAAAAAAAAAAAAAAA' | tr '0123456789' '9999999999' > "${OUT}"