From 1ae0544bf571a8fbf73bc29dd8b3c2ff62edb1a4 Mon Sep 17 00:00:00 2001 From: idankars Date: Sat, 8 Feb 2025 12:21:01 +0200 Subject: [PATCH] Idan Kars answers exercise 10 --- q1.sh | 21 ++++++++++++++++++++- q2.sh | 17 +++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100755 q2.sh diff --git a/q1.sh b/q1.sh index f7ac848..9d0bdb7 100644 --- a/q1.sh +++ b/q1.sh @@ -1 +1,20 @@ -This is q1 answer +echo "question 1.a" +awk '{print NR}' aliceinwonderland.txt | tail -n 1 +echo: "3325 rows" + +echo "question 1.b" +awk '{for(i=1;i<=NF;i++) {if($i ~ /^Alice$/) count++}} END {print count}' aliceinwonderland.txt +echo "212 times, note: in order to find only with spaces before and after, I searched for exactly the word Alice, with no extra characters" + + +echo "question 1.c" +awk '{for(i=1; i<=NF; i++) {gsub(/[^[:alpha:]]/, "", $i) words[$i]++}} END {for(word in words) {if(words[word] == 1) count++}} END {print count}' aliceinwonderland.txt +echo "1526 words, note that I removed punctuation, another approach is to look for words containing only alphabetic characters if($i ~ /^[[:alpha:]]+$/)" + +echo "question 1.d" +awk '{for(i=1;i<=NF;i++) if($i ~ /^[[:alpha:]]+$/) print $i}' aliceinwonderland.txt | sort | uniq -c | sort -k 1 -h | tail -n 5 | awk '{print $2}' +echo "of, a, and, to, the" + +echo "question 1.e" +awk '{for(i=1; i<=NF; i++) {if($i ~ /^[[:alpha:]]+$/) {lengths += length($i);count++;}}} END {print lengths / count}' aliceinwonderland.txt +echo "3.84393" diff --git a/q2.sh b/q2.sh new file mode 100755 index 0000000..bfd053f --- /dev/null +++ b/q2.sh @@ -0,0 +1,17 @@ +echo "question 2.a" +sed -n '/Holmes\|Sherlock/p' sherlockholmes.txt | wc -l +echo "463 lines" + +echo "question 2.b" +sed -n '/Holmes\|Sherlock/p' sherlockholmes.txt | grep -Eo 'Sherlock|Holmes' | wc -l +echo "554 times" + +echo "question 2.c" +sed -E 's/^/Hello: /' sherlockholmes.txt + +echo "question 2.d" +sed -E 's/[A-Z][a-z]+\s[A-Z][a-z]+/Idan/' sherlockholmes.txt + +echo "question 2.e" +sed -E 's/\(/[/g' sherlockholmes.txt | sed -E 's/\)/]/g' +