In [7]:
%%sh
cat << EOF > question2.sh
#!/bin/bash
echo "Science-related directories in /home/data/20_newsgroup/:"
ls /home/data/20_newsgroup/ | grep -i sci
echo "There are \`ls /home/data/20_newsgroup/ | grep -ic sci\` directories containing articles related to science."
EOF
In [8]:
%%sh
cat << EOF > question3.sh
#!/bin/bash
echo "Science-related directories in /home/data/20_newsgroup/:"
ls /home/data/20_newsgroup/ | grep -i sci
echo "There are \`ls /home/data/20_newsgroup/ | grep -ic sci\` directories containing articles related to science."
echo "There are \`ls /home/data/20_newsgroup/ | grep -icv sci\` directories NOT containing articles related to science."
EOF
In [10]:
%%sh
cat << EOF > question4.sh
#!/bin/bash
grep -iw water | tr 'A-Z' 'a-z' | tr 'atie' '4713' > leetlines.txt
echo "\`wc -l < leetlines.txt\` lines written."
EOF
In [13]:
%%sh
cat << EOF > question5a.sh
#!/bin/bash
grep -io "sing" | wc -l
# be careful! the question doesn't ask you to count lines with SING in them,
# it asks for occurence counts!
EOF
In [14]:
%%sh
cat << EOF > question5b.sh
#!/bin/bash
grep -iow sing | wc -l
# be careful! the question doesn't ask you to count lines with word SING in them,
# it asks for occurence counts!
EOF
In [20]:
%%sh
cat << EOF > question6.sh
#!/bin/bash
# (optional) just a quick check to make sure the right number of arguments is passed
if [ "\$#" -ne 2 ]; then
echo " Error[] Usage: questions6.sh <filename> <string>"
exit 1
fi
# rename the arguments: f = file, s = string
f=\$1
s=\$2
# count occurences of string s in file f
grep -io "\${s}" \${f} | wc -l
EOF
In [ ]: