Skip to content

Instantly share code, notes, and snippets.

@zhongleqi
Created January 18, 2023 06:54
Show Gist options
  • Select an option

  • Save zhongleqi/2b9492f370e302ef42a85ff1a2cb7e98 to your computer and use it in GitHub Desktop.

Select an option

Save zhongleqi/2b9492f370e302ef42a85ff1a2cb7e98 to your computer and use it in GitHub Desktop.
Leqi Zhong, Programming in Journalism, Assignment 0. Jan. 17, 2023
#!/bin/bash
# Silence the output of curl and write the results to specified file
curl -s --output banklist.csv https://www.fdic.gov/resources/resolutions/bank-failures/failed-bank-list/banklist.csv
# TODO: create ca_failed_banks.csv
# It must have the same header row as the original file (banklist.csv)
# Check out exercises/bash_drill.md (in this GitHub repo online)
# for pointers on getting the first line of a file
# and redirecting the content to a new file.
# WRITE THE COMMAND(S) HERE
# To start with, I would like to see the path of the banklist.csv
pwd ca_failed_banks.csv
# So I confirmed that it exists in my laptop. And I would like to see the first few lines to have a sense what am I doing.
head banklist.csv
# Now I'm moving to creat the ca_failed_banks.csv
which curl
mkdir ~/code/
# It told me the file exissts. I'm now proceed to create the file.
touch ca_failed_banks.csv
cat ca_failed_banks.csv
# It's a new file, so it has nothing. Now let's see the first line of banklist.csv
head -1 banklist.csv
# Write the line into ca_failed_banks.csv
echo head -1 banklist.csv > ca_failed_banks.csv
# See if it works?
cat ca_failed_banks.csv
# Done!!
# TODO: Next, filter banklist.csv for just the CA banks
# and APPEND those rows to the new file (ca_failed_banks.csv)
# WARNING: Make sure you append as opposed to overwriting the data!
# Again, you can get pointers in exercises/bash_drill.md (in this GitHub repo online)
# WRITE THE COMMAND HERE
# Search CA banks. I'll use quotation marks to make sure they are CA banks rather than names contain "CA". Also, I'm using -n, so it'll print and number lines that match, and I can check.
grep -n -w "CA" banklist.csv
# The second step is to append the rows to the ca_failed_banks.csv. (Actually I can combine two steps...)
grep -n -w "CA" banklist.csv >> ca_failed_banks.csv
# And let's check the result we have.
cat ca_failed_banks.csv
# Okay I think I'm done!!
# Some hackery that subtracts 1 from the overall file count of ca_failed_banks.csv
# and then spits out a message with the count on the command line.
ROW_COUNT=`wc -l < ca_failed_banks.csv | xargs` # HACK to strip whitespace
# SUBTRACT THE HEADER ROW FROM FINAL COUNT
NUM_BANKS=$(($ROW_COUNT - 1))
echo "There are ${NUM_BANKS} failed banks in California"
# I believe we can also use "wc -l" to count the number of lines.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment