Lab 06 Avec Solution
Lab 06 Avec Solution
Module 1 :
2. You find in your home directory : Characters, Numbers and MultiFieldChars files. Sort these files
by using sort command by default. Then, apply sort with options : -b, -f, -d, -u, -r, -n , -k, -t.
Solution.
Will be solved within the lab session.
3. You find in your home directory the file calories.csv, it is a file with csv format with semicolon
(“’;”) used as a separator. Replace semi colon marks (;) with apostrophes (‘) in file calories.csv,
and save the changes in calories.a.csv.
Solution.
tr \ ; \’ < calories.csv > calories.a.csv
5. Select the first column from file calories.csv, i.e., the column with food names.
Solution.
cut -sf1 -d \; calories.csv
6. In file calories.csv change capital letters to small letters, but only in the first column (cut the first
column using cut, replace capital letters with small ones using tr, then put them back using
paste).
Solution.
cut -f1 -d “ “ calories.csv > first
rm first others
7. In file calories.csv replace the letter (E) with (z), using diff then determine which lines have been
changed.
Solution.
tr “E” “z” < calories | diff calories.csv -
8. Sort file calories.csv according to amount of calories in increasing order. The first header line has
to remain at the beginning of the file.
Solution.
# save the header
head -n 1 calories.csv > header
mv header calories.csv
9. Sort file calories.csv according to triple [protein, carbohydrates, fat] in an increasing order. The
header has to remain at the beginning of the file.
Solution.
# save the header
head -n 1 calories.csv > header
mv header calories.csv
10. Reorder the last three columns of file calories.csv in the opposite of proteins, the second
contains amount of carbohydrates and the third column contains amount of fat. Use cut and
paste.
Solution.
cut -sf5 -d \; calories.csv > fifth
mv novy calories.csv
11. Select the nine characters with permissions from the output of ls -l command.
Solution.
ls -l | tail -n +2 | cut -c2-10
Suppose you have three files, summand1, summand2 and sum, each of these files contains the
same number of lines, each line contains just a number. Compose these files to just one output
where each line has the following format : summand1 + summand2=sum.
Solution.
paste -d+= summand1 summand2 sum
12. Output the list of files in the current directory in the form of “size file name”. The list should be
created by filtering the output of ls -l and ls commands.
Solution.
ls -l | tail -n +2 | tr -s “ “ | cut -d “ “ -f5 > lsl
ls | paste -d “ “ lsl –
rm lsl
13. Output the logins from file /etc/passwd, each contains five logins separated with a comma (,)
Solution.
cut -d: -f1 < /etc/passwd | paste –d “,” - - - - -
or
14. Split calories.csv into sub-files, each of which has 100 bytes. View some of them and than delete
them all using one command.
Solution.
split –b 100 calories.csv
cat xaa
rm x*
15. Split calories.csv into sub-files, each of which has 3 lines and starts with a prefix of your choice.
View some of them and than delete them all using one command.
Solution.
split –l 3 calories.csv new
cat newaa
rm new*
wc -l calories.csv
wc -w calories.csv
wc -c calories.csv
17. Output the five logins and UIDs from file /etc/passwd, and save the output to filea file.
Solution.
cut -d: -f1,3 < /etc/passwd | head -5 > filea
18. Output the three logins and UIDs from file /etc/passwd, and save the output to fileb file.
Solution.
cut -d: -f1,3 < /etc/passwd | head -3 > fileb
20. Compare between the two filea and fileb files and display their differences.
Solution.
cmp filea fileb