If you can see this check that

Main Page


Basic File Handling


A quick introduction to sort and more pipe examples.

To reset all the check buttons from a previous attempt click here

Question 1: Sort

The next few questions check your understanding of the sort command. Sort can read from a pipe or from a specified filename. The basic way sort works is to sort on a column where the columns are separated by spaces, so a file names.txt like:

gordon  russell
jim     jackson
bob     ludwiniak
can be sorted by the first column easily by doing
sort names.txt
If you want to sort by a column other than 1, you have to select the column. This is called the key, and has a start and end column numbered from 1. So to sort column 2 you need to do "-k" then the start and end column with a comma between them, i.e.:
sort -k 2,2 names.txt

To practice, press the button below to create a file "stuff.txt" in /home/caine.

Tests - not attempted
Create stuff.txt UNTESTED

Sort stuff.txt into alphanumeric order based on column 1 (the md5 value). Save this into /home/caine/sort1.txt.

Tests - not attempted
stuff.txt still valid UNTESTED
sort1.txt is correct UNTESTED

Now sort stuff.txt by column 2, and save the output as sort2.txt.

Tests - not attempted
stuff.txt still valid UNTESTED
sort2.txt is correct UNTESTED

Introduce the "-u" flag to sort, which removes any duplicate entries (such as /bin/ls in out example file). The 'u' stands for 'unique'. So now sort stuff.txt by column 2, but make sure all rows are unique. Save the output to sort3.txt.

Tests - not attempted
stuff.txt still valid UNTESTED
sort3.txt is correct UNTESTED

In these examples the character which split column 1 from column 2 was the space character. This is called the column delimiter. Sometimes columns will be separated with other characters, such as "," for comma separated files. So for instance if names.txt was:

can be sorted by the first column easily by doing
sort -t"," names.txt
and can be sorted by the second column easily by doing
sort -t"," -k 2,2 names.txt

Sort /etc/passwd (delimited by the ":" character) by column 5 and save that output into sort4.txt.

Tests - not attempted
sort4.txt is correct UNTESTED

If you are sorting a column which is composed of numbers, you need to decide if you want alphanumeric or numeric ordering. In alphanumeric ordering, "10" comes before "9" as the first character of "10" has a lower ascii value than "9". If you want numeric ordering where "10" is higher than "9", you must specify this in the "-k" command after each column number using the trailing character "n". So to sort column 10 numerically you need to do: "-k 10n,10n"

Firstly sort /etc/passwd on column 3 alphanumerically, saving the output to sort5.txt. Confirm that the last line of sort5.txt has a 9 in column 3.

Tests - not attempted
sort5.txt is correct UNTESTED

Repeat the sort, but this time sort numerically and save the output into sort6.txt. Confirm that the last line of sort6.txt has the number 65534 in column 3.

Tests - not attempted
sort6.txt is correct UNTESTED

Question 2: using grep

Use grep on /usr/share/dict/words to find the first word that contains the three letter sequence wta
wta word:

Tests - not attempted
Dictionary search UNTESTED

Question 3: count with grep

Use grep piped through wc on file /usr/share/dict/words to find the number of words that contain the letter x.
ber words:

Tests - not attempted
x word count UNTESTED

Question 4: negative grep

Use grep to find all lines in /etc/passwd that do not have nologin one the line. Make grep include the line numbers of the matching lines and send the output to file s5

Tests - not attempted
not nologin UNTESTED

Question 5: grep and ls

Use ls -l and grep to find all the files in the directory /etc that were last modified in August (hint: try looking for the case sensitive string "Aug"). Send this list to s6.

Tests - not attempted
ls and grep UNTESTED

Question 6: ls grep and sort

Use ls -l and grep and sort to find all the files in /etc that were last modified in Jan. Sort this list in descending order of size and then alphabetically by name (so 2 files with the same size will appear in alphabetic order). Send the output to s7. Sorting using other techniques will probably not get the same answer...

Tests - not attempted
Jun files in order UNTESTED

Centos 7 intro: Paths | BasicShell | Search
Linux tutorials: intro1 intro2 wildcard permission pipe vi essential admin net SELinux1 SELinux2 fwall DNS diag Apache1 Apache2 log Mail
Caine 9.0: Essentials | Basic | Search | SysIntro | 5a | 5b | 5c | 6 | 7a | 7b | 8a | 8b | Browser
CPD: Cygwin | Paths | Files and head/tail | Find and regex | Sort | Log Analysis
Kali: 1a | 1b | 1c | 2 | 3 | 4a | 4b | 5 | 6 | 7a | 8a | 8b | 9 | 10 |
Useful: Quiz | Forums | Privacy Policy | Terms and Conditions
Site Links:XMLZoo ActiveSQL ProgZoo SQLZoo

Linuxzoo created by Gordon Russell.
@ Copyright 2004-2018 Edinburgh Napier University