Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Settings
- fizz=3
- buzz=5
- input_file="$1"
- # Create an empty array
- numbers=()
- if [ "$1" = "" ] ; then
- echo "Usage: $0 <input file>"
- exit 1
- fi
- while read line ; do
- # Awk is a great shortcut tool for splitting a file into columns, but quotes mess it up
- col1="$(echo "$line" | awk '{ print $1 }')"
- # BASH has built in string replacement, but it can't do this yet...
- # ... so use sed to strip out anything that isn't numeric
- number="$(echo "$col1" | sed 's/[^0-9]*//g')"
- numbers+=($number)
- # Increment the line number for the next loop
- let line_number++
- done < "$input_file"
- # Print out the numbers, or fizz/buzz
- for number in ${numbers[@]} ; do
- finished=0
- let "${number}%${fizz}==0" && echo -n "fizz" && finished=1
- let "${number}%${buzz}==0" && echo -n "buzz" && finished=1
- if [ $finished -gt 0 ] ; then
- echo ""
- else
- echo "$number"
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement