Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions my_q1.sh
Original file line number Diff line number Diff line change
@@ -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


15 changes: 15 additions & 0 deletions my_q2.sh
Original file line number Diff line number Diff line change
@@ -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