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
87 changes: 86 additions & 1 deletion q1.sh
Original file line number Diff line number Diff line change
@@ -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"


















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