In: Computer Science
please fill in the blanks using c program or lunix with the answers being highlighted
1.
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?__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
Please find the answers below.
Question 1: diff telnos telnos2
What was the result?
4d3
< Cross Joseph MS 744-0320
Explanation:
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.
Question2: diff telnos telnos3
What was the result ?
2a3
> Smith John Comsc 744-4444
Explanation:
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.
Question3: diff telnos telnos4
What was the result?
2c2
< Harris Thomas Stat 744-7623
---
> Smith John Comsc 744-4444
What lines do you need to change in order to make the two files alike?
Please change 2nd line of `telnos` similar to 2nd line of `telnos4`.
Explanation:
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.
Question4: copy telnos to telnos5. What command did you use to do this?
cp telnos telnos5
Question5: diff telnos5
telnos2
What was the answer that you received?
4d3
< Cross Joseph MS 744-0320
Question6: 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?
- vim -f telnos5 telnos2
- Press "Esc" and "i" to insert
- Now edit the "telnos5"
What was the result?
As a result, Both telnos5 and telnos2 are similar.
What is the output of the diff command when the files match?
No response
Question:
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?
Cross Joseph MS 744-0320
Cross Joseph MS 744-0320
Davis Paulette Phys 744-9579
Davis Paulette Phys 744-9579
Hale Elizabeth Bot 744-6892
Hale Elizabeth Bot 744-6892
Harris Thomas Stat 744-7623
Harris Thomas Stat 744-7623
Holland Tod A&S 744-8368
Holland Tod A&S 744-8368
Smith John Comsc 744-4444
Question:
Next issue the command:
uniq tel2
What was the result?
Cross Joseph MS 744-0320
Davis Paulette Phys 744-9579
Hale Elizabeth Bot 744-6892
Harris Thomas Stat 744-7623
Holland Tod A&S 744-8368
Smith John Comsc 744-4444
Question:
Issue the command:
uniq -u tel2
What was the result?
Smith John Comsc 744-4444
Question:
Next, issue the command:
uniq -d tel2
What was the result?
Cross Joseph MS 744-0320
Davis Paulette Phys 744-9579
Hale Elizabeth Bot 744-6892
Harris Thomas Stat 744-7623
Holland Tod A&S 744-8368