Lab 12
Copy the file "aminegg.txt" from the link aminegg.txt to your directory on SNOWBALL.
Part 1
Try the following sed commands:
sed 's/ he/ HE/' < aminegg.txt
sed 's/ he/ HE/g' < aminegg.txt
sed 's/^/AAA/' < aminegg.txt
sed 's/$/ZZZ/' < aminegg.txt
Observe the results. What this does is call "sed" to "s"ubstitute one pattern
(such as " he" in the first example) with the second pattern (e.g. " HE").
The default is to do this only for the first occurrence, but you can over-ride
that with the "g" in the second example. To see the difference, look at the
line starting with "village, and" for both the first and second examples.
Next, use sed on that file to add START to the beginning of each line. Try this first by outputting the result to the screen. When you have it right, pipe it to sed a second time, adding the EOL to the end of each line. Make sure this works, show it in the script that it works. Hint: in sed, the carat (^) matches to the beginning of the line, while the dollar-sign ($) matches to the end of the line.
Next, do the same thing as above (use the up arrow to go to a previous command), and pipe (make sure you pipe the result from previous command) the result to "grep -v STARTEOL". The "-v" option for grep is a reverse, so any line that contains the text "STARTEOL" is not printed. (Use the "|" and cat to display the results)
Next, do the same thing as above (use the up arrow to go to a previous command), and modify it to write the output in the new file named yourUserName_aminegg.txt (This should create a new file in your working directory)
Next run the cmp utility using aminegg.txt and file you have created. Explain what does the output denotes, what option this utility can take and what it does for the utility? (use echo to explain your answer or you can type in txt file and cat it at this point)
Next run the diff utility, using aminegg.txt and file you have created.
Next, pipe the output to sed again, this time getting rid of START at the beginning, then pipe it again to sed, getting rid of EOL at the end of the file. To be clear, a line reading "START A EOL B START C EOL" should only have the first "START" and last "EOL" removed. This has the effect of removing any lines that are blank (though there are arguably better ways to do this). Confirm that the output is the same as the original file, except without the blank lines. (Use the cat command with pipe "|", to display the result)
Next, do the same thing as above (use the up arrow to go to a previous command), and modify it to overwrite the output in the file you created yourUserName_aminegg.txt
Next run the diff command, using aminegg.txt and file you have created. Explain what does the output shows (explain first 3 lines of output in detail? (use echo to explain your answer or you can type in txt file and cat it at this point)
Part 2
Use commands to print current date and time, total number of files (count) in your current working directory, print your working directory (path) and finally list all the files in the current directory.
Part 3
An example shell script (also called a shell program) can be found here.
Create a shell program to do the commands from the previous two parts, and call it "lab12_acct.sh", where you replace "acct" with whatever your account name is. Use "chmod" on it to allow you to execute it.
Start a log file with the "script" command, like "script lab12.log". Use "cat" to show the initial input file, and the shell script that you made.
Run the shell script.
Turn in the shell script, and any files created in this lab.
Make sure that all of the above is shown in the log file. Also, remove any non-printable characters before turning it in. You may want to call the new version "lab12.txt"
When you are ready to turn this in, remember to follow the the directions on turning in labs/homeworks.