diff --git a/q1.sh b/q1.sh index f7ac848..b6cb759 100644 --- a/q1.sh +++ b/q1.sh @@ -1 +1,86 @@ -This is q1 answer +#!/bin/bash +#q1.sh + +file="aliceinwonderland.txt" + +# question a + +awk 'END { print "The book has", NR, "rows" }' "$file" + +# question b + +echo + +awk '{ total += gsub(/ Alice /, "") } END { print "The word Alice appears", total, "times" }' "$file" + +# question c + +echo + +awk ' +{ + for (i = 1; i <= NF; i++) { + count[$i]++ + } +} +END { + print "Words that appear only once:" + for (word in count) { + if (count[word] == 1) + print word + } +} +' "$file" + +# question d + +echo + +echo "The top 5 most common words in the book are:" + +awk ' +{ + for (i = 1; i <= NF; i++) { + count[$i]++ + } +} +END { + for (word in count) { + printf "%d %s\n", count[word], word + } +} +' "$file" | sort -nr | head -5 + +# question e + +echo + +awk ' +{ + for (i = 1; i <= NF; i++) { + tot_length += length($i) + tot_words++ + } +} +END { + print "The average word length in the book is", tot_length / tot_words +} +' "$file" + + + + + + + + + + + + + + + + + + diff --git a/q2.sh b/q2.sh new file mode 100644 index 0000000..02c60c4 --- /dev/null +++ b/q2.sh @@ -0,0 +1,24 @@ +#!/bin/bash +#q2.sh + +file="sherlockholmes.txt" + +# question 1 + +sed -n '/Sherlock\|Holmes/p' "$file" | wc -l + +# question 2 + +sed 's/ /\n/g' "$file" | grep -c -E 'Sherlock|Holmes' + +# question 3 + +sed 's/^.\{71,\}$/Long line/' "$file" + +# question 4 + +sed 's/\<[A-Z][A-Za-z]\{2,\} [A-Z][A-Za-z]\{2,\}\>/Omri Naftali/g' "$file" + +# question 5 + +sed 's/(\([^)]*\))/[\1]/g' "$file"