Substitute: Correct user-data.csv

Correct user-data.csv

The user-data.csv file we downloaded from https://launchcodelearning.org/api/walkthrough/user?data_format=csv has a couple of mistakes we need to fix across the entire file.

  1. Typo: company mastercard should be Mastercard
  2. Typo: company spectrum should be Spectrum
  3. Company Stephens-Griffin name changed to: Stephens-Griffin-Ferguson

sed substitute ‘mastercard’ with ‘Mastercard’

Run a substitute command:

sed 's/mastercard/Mastercard/' user-data.csv

Output:

sed ’s/mastercard/Mastercard/’ user-data.csv output

However, this doesn’t alter the file it simply displays the substitution in STDOUT.

Bonus

You can see that the change didn’t take place by running grep 'mastercard' user-data.csv:

grep ‘mastercard’ user-data.csv output

Write Changes

Write the changes to a new file named user-data.corrected.csv. This way the original file stays unchanged.

sed 's/mastercard/Mastercard/' user-data.csv > user-data.corrected.csv

Validation

Since the substitution is writing to a file instead of routing to STDOUT the change is not seen automatically. There are many tools that allow you to view the file so the changes can be validated:

grep 'Mastercard' user-data.corrected.csv

Output:

grep ‘Mastercard’ user-data.corrected.csv

Bonus

Since you have access to both the original file and corrected file you could use git diff to view the differences between the two files:

git diff user-data.csv user-data.corrected.csv

Output:

git diff user-data.csv user-data.corrected.csv output

You can see the exact lines that were deleted and the lines they were replaced with. This is a less screen you can navigate with the arrow keys, j (up) and k (down) keys and type q to exit.

sed substitute ‘spectrum’ with ‘Spectrum’

The file needs more corrections.

Run a substitute command & write:

sed -i 's/spectrum/Spectrum/' user-data.corrected.csv

Validation

grep 'spectrum' user-data.corrected.csv

We should not see any records as the instances of spectrum were replaced by Spectrum

Bonus

We leave it up to you to run grep 'Spectrum' user-data.corrected.csv or git diff user-data.csv user-data.corrected.csv to further confirm the changes.

sed substitute ‘Stephens-Griffin’ with ‘Stephens-Griffin-Ferguson’

Run a substitute command & write:

sed -i 's/Stephens-Griffin/Stephens-Griffin-Ferguson/' user-data.corrected.csv

Validation

grep 'Stephens-Griffin-Ferguson' user-data.corrected.csv

Output:

grep ‘Stephens-Griffin-Ferguson’ user-data.corrected.csv output

Bonus

You can run any sed scripts and edit the file in place by using the -i flag. This way you wouldn’t need to read a file and then write out the same file.

Instead of:

sed 's/Stephens-Griffin/Stephens-Griffin-Ferguson/' user-data.corrected.csv > user-data.corrected.csv

This works:

sed -i 's/Stephens-Griffin/Stephens-Griffin-Ferguson/' user-data.corrected.csv