diff --git a/my_q1.sh b/my_q1.sh new file mode 100644 index 0000000..81b25c7 --- /dev/null +++ b/my_q1.sh @@ -0,0 +1,40 @@ +# first question: +awk 'END {print "Total lines:", NR}' aliceinwonderland.txt + +#second question: +awk '{for (i=1; i<=NF; i++) if ($i ~ /Alice/) count++} END {print "Alice appears:", count, "times"}' aliceinwonderland.txt + +#third question: +awk '{for (i=1; i<=NF; i++) words[tolower($i)]++} END {print "Unique words:", length(words)}' aliceinwonderland.txt + +#fourth question: +awk '{ + for (i=1; i<=NF; i++) { + word = tolower($i) + gsub(/[^a-z]/, "", word) + if (word != "") words[word]++ + } +} +END { + for (w in words) print words[w], w +}' aliceinwonderland.txt | sort -nr | head -5 + +#fifth question: +awk '{ + for (i=1; i<=NF; i++) { + word = tolower($i) # Convert word to lowercase + gsub(/[^a-z]/, "", word) # Remove all non-letter characters + if (word != "") { # Ignore empty words + total_length += length(word) # Sum word lengths + word_count++ # Count number of words + } + } +} +END { + if (word_count > 0) + print "Average word length:", total_length / word_count + else + print "No words found." +}' aliceinwonderland.txt + + diff --git a/my_q2.sh b/my_q2.sh new file mode 100644 index 0000000..adbb2db --- /dev/null +++ b/my_q2.sh @@ -0,0 +1,15 @@ +#first question: +sed -n '/\b\(Sherlock\|Holmes\)\b/p' sherlockholmes.txt | wc -l + +#seond question: +sed -n 's/\b\(Sherlock\|Holmes\)\b/&\n/gp' sherlockholmes.txt | wc -l + +#third question: +sed 's/^/Hello: /' sherlockholmes.txt + +#forth question: +sed -E 's/\b[A-Z][a-z]+ [A-Z][a-z]+\b/Bar/g' sherlockholmes.txt + +#fifth question: +sed -E 's/\(/[/g; s/\)/]/g' sherlockholmes.txt +