Week 2 assignment

I’ve been trying the week 2 quiz for observers and for the questions about user manuals find that I get answers that aren’t even options to submit! I appreciate this may be because the manuals differ between linx/mac/windows. However, I’d appreciate seeing any code others have written which does seem to work. Or perhaps the instructors could share some model code answers? (I know the assignment isn’t graded, but would still like to be able to understand the answers!)

First Q:
My attempt at counting occurences of pattern within grep user manual is
man grep | grep pattern | wc -l
(output 21 in windows ubuntu, 23 in linux ubuntu)

Second Q:
Using only commands in the Terminal application, create a directory called “week_2”. Within this directory, save the user manuals of the commands “grep”, “cat”, and “ls” to respective files called “grep.txt”, “cat.txt”, and “ls.txt”. Using one line in the Terminal, sort the contents of these three files by the number of lines they contain, in ascending order. What is this order?

Part 1: making files
mkdir week_2

cd week_2

man grep > grep.txt

man cat > cat.txt

man cat > ls.txt

Part 2. I really struggled to get this all on one line.
for f in *.txt; do wc -l $f >> lines.txt ; done
sort -n lines.txt

(output: 71 cat.txt, 71 ls.txt, 671 grep.txt)

Your intuition is spot on for both questions! Yes, this is likely an issue with the manual differing between operating systems. Sorry for this confusion!

First Q: That is one of the correct commands.

Second Q: Using a for loop is one way to go, as is done in the lecture. If you want to get this all done in one line (and not write lines.txt to disk), you could pair *.txt with wc and pipe that to sort

Thanks, Dustin.

For Q2 wc -l *.txt | sort -n is certainly neater, even though I get the same output (not as per answers).

For Q1 I think I’m going to need another hint. I’ve had a look through the man grep and couldn’t see an option that allowed wildcards before or after the text…not sure what next to try

Sorry if I wasn’t clear, man grep | grep pattern | wc -l is correct, you don’t need wildcards for this command.

Another option is a two step pipe (instead of the 3 step pipe in man grep | grep pattern | wc -l) that uses the -c option in grep.