Skip to content

Instantly share code, notes, and snippets.

@chrismullins
Created December 31, 2013 06:21
Show Gist options
  • Save chrismullins/8193311 to your computer and use it in GitHub Desktop.
Save chrismullins/8193311 to your computer and use it in GitHub Desktop.
Bash script to randomly permute every occurrence of a word in a file.
#!/bin/sh
Perm () {
A=$1
i=0
while [ $i -lt ${#A} ]; do y[$i]=${A:$i:1}; i=$((i+1));done
s=`shuf -e ${y[@]}`
newString=''
s=$(sed 's|\ ||g' <<< $s)
i=0
while [ $i -lt ${#s} ];
do
nextChar=${s:i:1}
newString+=$nextChar
i=$((i+1))
done
eval "$2='$newString'"
}
# USAGE
# Perm $old_string new_string
if [ "$#" -ne 3 ]; then
echo "ERROR, ILLEGAL NUMBER OF ARGUMENTS"
echo "USAGE: ./string_scramble.sh inFile.txt string_to_randomize outFile.txt"
exit 1
fi
while read LINE
do
num=`echo $LINE | grep -o "$2" | wc -l`
replace_string=''
j=1
num=$((num+1))
while [ $j -lt $num ];
do
Perm $2 replace_string
newline=$(sed 's|'$2'|'$replace_string'|1' <<< $LINE)
LINE=$newline
j=$((j+1))
done
echo $LINE >> $3
done < $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment