Lesson 3: Advanced Command Line¶
Jumping in the Deep End¶
At the beginning of this course, we said that we believe in active learning. Below are a bunch of exercises in roughly increaisng order of difficulty. Here’s a bunch of advanced command line info you might find useful, though Google is probably your best friend.
Behind the Scenes: You might wonder, “Why are we being given tasks that we haven’t been taught how to complete?” The answer: that’s science. One of the most powerful skills in bioinformatics is simply being able to parse documentation to figure out what you need to do. Taking the general knowledge from last lesson and applying it to scenarios you’ve never seen before is very, very valuable!
Get file1.txt and file2.txt:
wget --no-check-certificate 'https://docs.google.com/uc?export=download&id=1DozuwGq6nQRSf2Dx6VNXSv6Vha8OMCIM' -O file1.txt
wget --no-check-certificate 'https://docs.google.com/uc?export=download&id=17kYqxphGrkCK30FQuOUDIXOUhiZvjAMd' -O file2.txt
1. file1.txt contains the text of a Sherlock Holmes book. How many lines of the file does it take to recount his tale in Bohemia? Hint: The table of contents is form line 48 to 70
Stumped? Click me!
Does this command give you an idea of what you can search for?
head -n 70 file1.txt | tail -n 22
You can also use command
awk which is a great multipurpose effecitent text processing command.
awk "NR<70" file1.txt | awk "NR>48"
2. How many lines in file2.txt contain a period? (Hint: The answer is not 60)
3. Confirm your answers to 2-5 with an instructor or tutor. Then, delete the directory you downloaded in step 0.
Your Own File¶
So far, you’ve been working with files provided to you. However, for most things you’ll want to do moving forward, you’ll need to create your own. How do you do that?
We’re going to be using a text editor called “vim”. Think of vim as Microsoft Word, but for your command line. You can use vim to open a file containing text or create your own (just like Word!).
Vim is navigated through keyboard commands, which at first are confusing and mostly non-intuitive. However, once you become familiar with using these keyboard shortcuts, Vim quickly becomes one of the fastest text-editors for editing your code! (Note: An advanced user may also edit the .vimrc file to customize their local Vim to better fit your habits and/or adjust UI style.)
Here is a helpful interactive online resource on learning the vim keyboard commands: openVim
Here’s a walkthrough of exactly what you’d need to hit on your keyboard to create a file called “new.txt” that contains “hello there” inside of it:
vim new.txt i hello there 'esc' :x
Unsure what these commands do? Click me!
vim new.txt- Open a file called new.txt in vim. If no file exists in the directory (true for us!) create a new one
i- Moves you into insert mode (where you’re actually allowed to type)
hello there- Adds text into the file
esc- Moves out of insert mode
:x- Saves and quits the file
cat to confirm that you’ve succeeded in creating this file.
Create an executable file called
greet.sh that can be executed to print “Hello World” to the command line.
No idea where to start? Click me!
Tackle these subproblems:
- What is an executable file?
- How do I make a file print “Hello World”?
- How can I execute a file?
- [Optional, depending on if #3 fails] How can I make a file executable?
Update your program to print “Hello [your_username]”. This should NOT be hardcoded, meaning if your program prints “Hello jsmith” on your laptop, it should print “Hello smansuri” on mine without any changes.