Thinking in C and *nix (CSC 282 2015S) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [Teaching & Learning] - [Calendar]
Current: [Outline] [EBoard] [Lab] [Assignment]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines]
Reference: [EBook] - [ISO] [GNU Coding Standards] [GCC Documentation] - [TAoUP] [Make3]
Previous Offerings: [CSC 295 2013S] [CSC 295 2014S]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker]
Overview
&, |, etc.)Are your questions intended to be as open-ended as they seem?
Yes. I look forward to discussing interesting answers. We won't discuss all of them, though.
Problems
Given a DOS-formatted text file (lines end with \r\n rather than just \n), convert it to a standard text file.
Strategy 1: Write a C program. Try to remember the wonders of C I/O.
int c;
while ((c = getchar()) != EOF)
{
if (c != '\r')
putchar (c);
} // while
getchar needs to be able to signal a special status,
and returns a value outside the normal range of characters to
signal those special statuses. EOF is outside the normal range.char is an 8-bit integer, C promotes (converts) types as
necessary.Observation: Overwriting the file is difficult.
\r somewhere
else, it still deletes it. (Sam thinks that's okay.)Strategy two: Use tr (translate). Traditionally used to translate
one character to another. Can also delete characters if you use the
-d flag.
tr -d '\r' < original.dos > fixed.txt
Strategy 3: Use dos2unix, which is not a standard program. Five minutes.
Strategy 4: Find a more sophisticated text translation tool and translate
"\r\n" to "\n". The sed program is great for this
sed -e 's/\r\n/\n/g' < original.dos > fixed.txt
Warning! The following is a bad idea!
command < file.txt > file.txt
Observation: There are lots of flags of sed.
Given a standard text file, convert all uppercase letters to lowercase.
Given a standard text file, remove all blank spaces at the end of lines.
Sed is your friend.
sed -e 's/ *$//g' < infile.txt > outfile.txt
The '/g' is not necessary here, but is generally useful.
$ sed -e 's/a/A/'
Eh, I wish I were a canadian so that I could say eh.
Eh, I wish I were A canadian so that I could say eh.
$ sed -e 's/a/A/g'
Eh, I wish I were a canadian so that I could say eh.
Eh, I wish I were A cAnAdiAn so thAt I could sAy eh.
Make a list of all misspelled words in a text file.
aspell list < file
aspell list < file | uniq
aspell list < file | sort | uniq
spell file | sort -u
Given a CSV file in which each line has the form
LastName,FirstName,Assignment,NumericGrade
find the the five highest grades on homework 2.
Hint: cut, sort, head
Given an HTML file, find the URLs of all images. In case you don't know
HTML, those will typically look like <img ... src="*URL*" ...> The
img can have any capitalization (img, IMG, Img, iMg, etc.)
There can be other text between the img and the src. (That text
cannot include a greater than sign.)
Learn Markdown so that we can use it for these assignments.
Learn about how you set and get environment variables. Put your explanations in your personal directory for the assignment.
Come up with two or three tasks, similar to those we did for assignment 1, and put notes about them in the tasks directory. (You can include some sample solutions in your file.)
Read the first chapter of the Make book.