0% found this document useful (0 votes)
22 views4 pages

Lab 06 Avec Solution

Uploaded by

heroefootball
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

Lab 06 Avec Solution

Uploaded by

heroefootball
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1st year of the first cycle Higher School of Computer Science

Course : Introduction to operating system 1 Sidi Bel Abbes (ESI-SBA)

Lab assignment N°6


Linux Filters

Module 1 :

1. Use the script utility to capture your session.


Solution.
script Lab6

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

4. Remove apostrophes (‘) from file calories.a.csv.


Solution.
tr -d \’ < 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

cut -sf2- -d \; calories.csv > others

tr ‘[:upper:]’ ‘[:lower:]’ < first | paste -d \; - others > calories.csv

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

# sort the rest and append it to header

tail –n +2 calories.csv | sort –t \; -k4,4 –nr >> 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

# sort the rest and append it to header

tail –n +2 calories.csv | sort –t \; -k7,7n –k6,6n –k5,5n >> 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

cut –sf6 -d \; calories.csv > sixth

cut –sf7 -d \; calories.csv > seventh

cut –sf1-4 –d \; calories.csv | paste –d \; - seventh sixth fifth > novy

mv novy calories.csv

rm fifth sixth seventh

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

cut -d: -f1 < /etc/passwd | paste -s –d “,,,,\n” -

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*

16. Apply wc command to calories.csv with and without options.


Solution.
wc calories.csv

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

19. Display the common lines between filea and fileb.


Solution.
Remark : before applying comm. command for comparison between files, you have to firstly sort
these files.

comm filea fileb

20. Compare between the two filea and fileb files and display their differences.
Solution.
cmp filea fileb

diff filea fileb

21. Exit the script.


Solution.
exit

You might also like