6.0 EDIT, SORT, CUT & GREP
Author: Dr. Alejandra Rougon
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
🔍 Learning Objectives
After completing this lesson you will learn how to:
- Edit files in the command line
- Sort file contents alphabetically or numerically
- Cut columns from files
- Look for specific strings inside files
You have already learn how to create a file with cat > New_file
, however, cat
is not a text editor. There are several text editors for the command line. The most common ones are vi
, vim
and nano
. In your virtual terminal you have a text editor with a graphical interface. However, when you work remotely on a bioinformatics computer cluster for High Performance Computing [HPC] you may not be able to use any graphical interface software. So it is very important that you learn how to use text editors in the command line.
vim
& vi
The text editors vim
and vi
are very similar. We will learn the most important commands for vim
and they will also work for vi
. However, if you want to learn more options you can check the vim documentation.
- Create file
vim Name_Of_File
- Edit file
i
which stands for insert - Save file and quit
Esc
:wq!
- Quit without saving
Esc
:q!
Now let's create a folder inside the folder Documents
named Practice
. Then go inside Practice
and create a file called numbers.txt

i
in order to be able to insert text. Now enter the following text:
Once you finish editing your file you have to save it and quit with the key Esc
followed by :wq!
and Enter
.

you can check the contents with less
, more
or cat
. Note: Don't add a >
when opening your file, otherwise you will erase previous contents and overwrite them.
nano
The advantage of using nano
is that you don't have to learn the menu, as it is shown on the screen. The key Ctrl
is specified by a caret ^
. The problem is that nano
sometimes is not installed in servers. So you may have to install it.
- Create file
nano Name_Of_File
- Save file
Ctrl
+o
Enter
- Quit
Ctrl
x
Let's create the file called toygenes.txt
in the same folder Practice
.
This time we will write a tabular file. That means our file will have columns and we will separate the columns with a tab
using the tab
key instead of the space bar
. Write the following contents (using one tab
after each name):
To save [write out] your file type in Ctrl
o
.
Then, you will see this screen
Just press Enter
and then quit with Ctrl
x
Remember to verify that the file is correct.
sort
The sort
command is used to sort a file, in other words, arranging the records on a list in a particular order. By default it gets sorted in alphabetical order.
Try it with the file toygenes.txt
sort
will also sort numbers in alphabetical order by default. So in order to sort numbers numerically we need de option -n
.
If you have a mixsure of letters and numbers you can use the option -V
to sort numerically as in software version names. As in the following example.
Now create a file called amounts.txt
with the following content:
The sort
command has several useful options. If you want to reverse the order use -r
-k
. For example let's sort toygenes.txt
based on the second column.
You can also sort a file and remove duplicates in order to get only the unique records with -u
. For example try it with the following file.
Remember that you can always save your results by redirecting the output with >
to a new file.
You can also sort months in order with sort -M
.
cut
The cut
command will cut specific columns that are separated by a tab
by default. You can specify the column with -f
, which stands for field. Select the second column for the file toygenes.txt
-d
. For example, for a list that has columns separated by a space
You can also select columns specifying the byte position with -b
. To see more options, remember that you can always check the manual man cut
.
grep
One of the most useful commands is grep
which allows you to look for strings. It will print only the lines that contain the string that you have indicated. For example, let's look for the string tomato
in the file toygenes.txt
-v
. It will show you the lines that don't contain the string.
You can also count the number of ocurrences of a string with -c
. How many lines contain a letter a
in toygenes.txt
?
🔑 In this lesson you have learned how to:
- Edit files in the command line with
vim
,vi
&nano
- Sort file contents alphabetically or numerically with
sort
- Cut columns from files with
cut
- Look for specific strings inside files with
grep