Guest User

Untitled

a guest
Jul 11th, 2020
1,541
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. URL="https://thedataweb.rm.census.gov/pub/cps/march/asec2018_pubuse.dat.gz"
  3. FILE="asec2018.dat"
  4.  
  5. # Download data file if not already present
  6. if [[ ! -f "$FILE" ]]; then
  7.     echo "Downloading $URL"
  8.     curl -s "$URL" -o "${FILE}.gz"
  9.     gunzip "${FILE}.gz"
  10. fi
  11.  
  12. # Walk through the file line by line, filling the counters
  13. IFS=$'\n'
  14. lines=( $(cat $FILE) )
  15. total_pop=0
  16. poor_pop=0
  17. for l in "${lines[@]}"; do
  18.  
  19.     # Ignore non-person records
  20.     RECORD="${l:0:1}"
  21.     if [ $RECORD -ne 3 ]; then
  22.         continue
  23.     fi
  24.  
  25.     # Ignore chilren below age 15 not living with family
  26.     FAMREL="${l:35:1}"
  27.     AGE="${l:18:2}"
  28.     if [ $FAMREL -eq 0 ] && [ $AGE -lt 15 ]; then
  29.         continue
  30.     fi
  31.  
  32.     # Add up total and poor population
  33.     WEIGHT="${l:154:8}"
  34.     PERLIS="${l:605:1}"
  35.     total_pop=$(bc <<<"$total_pop + $WEIGHT")
  36.     if [ $PERLIS -eq 1 ]; then
  37.         poor_pop=$(bc <<<"$poor_pop + $WEIGHT")
  38.     fi
  39. done
  40.  
  41. # Divide by 100 due to 2 implied decimal places
  42. total_pop=$(bc -l <<<"scale=2; $total_pop / 100")
  43. poor_pop=$(bc -l <<<"scale=2; $poor_pop / 100")
  44. poverty_rate=$(bc -l <<<"scale=1; $poor_pop*100 / $total_pop")
  45.  
  46. echo "Total Population: $total_pop"
  47. echo "Poor Population: $poor_pop"
  48. echo "Poverty Rate: $poverty_rate"
RAW Paste Data