Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- URL="https://thedataweb.rm.census.gov/pub/cps/march/asec2018_pubuse.dat.gz"
- FILE="asec2018.dat"
- # Download data file if not already present
- if [[ ! -f "$FILE" ]]; then
- echo "Downloading $URL"
- curl -s "$URL" -o "${FILE}.gz"
- gunzip "${FILE}.gz"
- fi
- # Walk through the file line by line, filling the counters
- IFS=$'\n'
- lines=( $(cat $FILE) )
- total_pop=0
- poor_pop=0
- for l in "${lines[@]}"; do
- # Ignore non-person records
- RECORD="${l:0:1}"
- if [ $RECORD -ne 3 ]; then
- continue
- fi
- # Ignore chilren below age 15 not living with family
- FAMREL="${l:35:1}"
- AGE="${l:18:2}"
- if [ $FAMREL -eq 0 ] && [ $AGE -lt 15 ]; then
- continue
- fi
- # Add up total and poor population
- WEIGHT="${l:154:8}"
- PERLIS="${l:605:1}"
- total_pop=$(bc <<<"$total_pop + $WEIGHT")
- if [ $PERLIS -eq 1 ]; then
- poor_pop=$(bc <<<"$poor_pop + $WEIGHT")
- fi
- done
- # Divide by 100 due to 2 implied decimal places
- total_pop=$(bc -l <<<"scale=2; $total_pop / 100")
- poor_pop=$(bc -l <<<"scale=2; $poor_pop / 100")
- poverty_rate=$(bc -l <<<"scale=1; $poor_pop*100 / $total_pop")
- echo "Total Population: $total_pop"
- echo "Poor Population: $poor_pop"
- echo "Poverty Rate: $poverty_rate"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement