In: Computer Science
please use linux or unix to complete
Diff command
The diff command displays differences between two files on a line-by-line basis. It displays the differences as instructions that you can use to edit one of the files ( using the vi editor) to make it the same as the other. When you use diff, it produces a series of lines containing Append (a), Delete (d), and Change (c) instructions. Each of these lines is followed by the lines from the file that you need to append, delete, or change. A less than symbol (<) precedes lines from file1. A greater than symbol (>) precedes lines from file2.
You will now need four files. These are telnos, telnos2, telnos3, telnos4. These files are all short files that contain names, departments, and telephone numbers. This is what they look like.
telnos |
telnos2 |
Hale Elizabeth Bot 744-6892 |
Hale Elizabeth Bot 744-6892 |
telnos3 |
telnos 4 |
Hale Elizabeth Bot 744-6892 |
Hale Elizabeth Bot 744-6892 |
To make it easier to copy you can use the * (wildcard) to copy these files. Type in the command:
cp /tmp/csc3321/telnos* .
Remember the . (period) means current directory and will copy all of the telnos files at one time and assign them the names that they have in the instructor's file
In order to see how diff works, type in:
diff telnos telnos2
What was the result?
________________________________________________________________________________________________________________________________________________________________________________________________________________________
The difference between these two files (telnos and telnos2) is that the 4th line in telnos is missing from telnos2. The first line that diff displays (4d3) indicates that you need to delete the 4th line from file telnos to make the two files match. The 4 is the line number and the (d) is delete. The line number to the left of each of the a,c, or d instructions always pertains to file1. Numbers to the right of the instructions apply to file2. The diff command assumes that you are going to change file1 to file2. The next line that diff displays starts with a less than (<) symbol indicating that this line of text is from file1. Next type in:
diff telnos telnos3
What was the result?__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
In this case, the second line has the (>) greater than sign which means that the extra line is in file2. The a means you must append a line to the file telnos after line 2 to make it match telnos3. Append means to add on to the end. Next is an example of the change feature. Type in the following command:
diff telnos telnos4
What was the result? __________________________________________________________
What lines do you need to change in order to make the two files alike? ________________________________________________________________________________________________________________________________________________
Notice that the three hyphens indicate the end of the text in the first file that needs to be changed and the start of the second file that needs to be changed. Next, copy telnos to telnos5. What command did you use to do this? ________________________________________________________________________
Next, type in:
diff telnos5 telnos2
What was the answer that you received? _________________________________________________________________________________________________________________________________________________________________________________________________________________
Use the vi editor to change telnos5 to match the file telnos2. Then check to see if they are now alike.
What command did you use? _________________________________________________________
What was the result? __________________________________________________
What is the output of the diff command when the files
match?
_______________________________________________________________________________
When the two files are alike, there is no response. Unfortunately, Unix is not always user-friendly.
Uniq Command
The uniq command displays a file, removing all but one copy of successive repeated lines. If the file has been sorted, uniq ensures that no two lines that it displays are the same. Sort telnos and telnos3 and then send them to a new file called tel2. Type in the following:
sort telnos telnos3 > tel2
Next look at the file, tel2. What does it contain?___________________________________________________________________________________________________________________________________________________________________________________________________
Next issue the command:
uniq tel2
What was the result?__________________________________________________________________________________________________________________________________________________________________________________________________________________
The uniq command has three options. These are:
-c Causes uniq to precede each line with the number of occurrences of the line in the input file
-d Displays only lines that are repeated
-u Displays only lines that are not repeated
Issue the command:
uniq -u tel2
What was the result?__________________________________________________________________________________________________________________________________________________________________________________________________________________
Next, issue the command:
uniq -d tel2
What was the result?__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
Grep Command
The grep command searches one or more files for a specified pattern. Normally each matching line is copied to the standard output. Two options that can be used with grep are:
-i
ignore
case of alphabetic characters
-n
precede each line printed by its relative line number in the input
file
Use the line numbers in the output of grep to
answer the questions in the following sections. Issue the
command:
grep -n H telnos
What was printed? ________________________________________________________________________________________________________________________________________________________________________________________________________________________
Issue the command:
grep -ni m telnos
What was printed this time?___________________________________________________________________________________________________________________________________________________________________________________________________________________
Telnos5 is not available
Telnos5 created using cp command
-d finds duplicate entries
-n, --line-number with H character in file telneos
-i, --ignore-case for ignore-case M or m inside file telnos file
Thanks