bash Overview
grep
The grep
utility is used for searching for regular expressions in files. There are many variants of the grep
command.
Examples
Use the grep
command to extract all of the lines from this file (/anvil/projects/tdm/data/icecream/combined/reviews.csv
) that contain the word terrific and store these reviews in a new file called terrificreviews.csv
in your home directory.
Click to see solution
%%bash
grep "terrific" /anvil/projects/tdm/data/icecream/combined/reviews.csv > $HOME/terrificreviews.csv
cut
We can use cut
to extract information from a text file. We usually just need to specify the delimited between the fields of data, using the -d
option, and we also need to specify the fields to extract, using the -f
option. For example, we can display the city and state of the donations to federal election campaigns.
Examples
From the file terrificreviews.csv
that you created in the previous example, how many of the reviews had only 1 star? How many had 4 stars? How many had 5 stars?
Click to see solution
%%bash
cut -d, -f5 $HOME/terrificreviews.csv | sort | uniq -c | sort -n
1 1 2 4 9 5 There are nine 5-star reviews, 2 4-star reviews, and one 1-star review.