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.
- Typo: company
mastercardshould beMastercard - Typo: company
spectrumshould beSpectrum - Company
Stephens-Griffinname changed to:Stephens-Griffin-Ferguson
sed substitute ‘mastercard’ with ‘Mastercard’
Run a substitute command:
sed 's/mastercard/Mastercard/' user-data.csvOutput:
However, this doesn’t alter the file it simply displays the substitution in STDOUT.
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.csvValidation
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.csvOutput:
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.csvOutput:
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.csvValidation
grep 'spectrum' user-data.corrected.csvWe 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.csvValidation
grep 'Stephens-Griffin-Ferguson' user-data.corrected.csvOutput:
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.csvThis works:
sed -i 's/Stephens-Griffin/Stephens-Griffin-Ferguson/' user-data.corrected.csv



